I've got a simple contactListener and the isThereAnyContact method below to check if there is a contact of my player with a STAR body (in order to get +1 points).
Now, this code seems to be working, as it seems to run correctly and all the NSLogs appear without any errors. However, the sprites remain and do not disappear. Am I mistaken or shouldn't they disappear when the '[self removeChild:sprite cleanup:YES];' is called?
Please help with any ideas or comments...
Thanks!
- (void)isThereAnyContact {
if (_gameOver) return;
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
if ((spriteA == _fish_player && spriteB.tag == STAR) ||
(spriteB == _fish_player && spriteA.tag == STAR)) {
_stars++;
[self updateStars];
NSLog(@"%d",_stars);
if(spriteA.tag == STAR) {
toDestroy.push_back(bodyA);
} else if(spriteB.tag == STAR) {
toDestroy.push_back(bodyB);
}
}
}
}
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) body->GetUserData();
NSLog(@"removed");
[self removeChild:sprite cleanup:YES];
}
NSLog(@"destroyed");
_world->DestroyBody(body);
body = NULL;
}
}
Now, this code seems to be working, as it seems to run correctly and all the NSLogs appear without any errors. However, the sprites remain and do not disappear. Am I mistaken or shouldn't they disappear when the '[self removeChild:sprite cleanup:YES];' is called?
Please help with any ideas or comments...
Thanks!
- (void)isThereAnyContact {
if (_gameOver) return;
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
if ((spriteA == _fish_player && spriteB.tag == STAR) ||
(spriteB == _fish_player && spriteA.tag == STAR)) {
_stars++;
[self updateStars];
NSLog(@"%d",_stars);
if(spriteA.tag == STAR) {
toDestroy.push_back(bodyA);
} else if(spriteB.tag == STAR) {
toDestroy.push_back(bodyB);
}
}
}
}
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) body->GetUserData();
NSLog(@"removed");
[self removeChild:sprite cleanup:YES];
}
NSLog(@"destroyed");
_world->DestroyBody(body);
body = NULL;
}
}