Hey guys, I implemented the MyContactListener as described in the Tutorial.
I also can remove my bullet object when it reaches the top of the screen.
But this isn't working anylonger when I add the Background to the scene, and I can't figure out why it's reacting that way :(
Here some Code:
-(void) tick: (ccTime) dt
{
//It is recommended that a fixed time step is used with Box2D for stability
//of the simulation, however, we are using a variable time step here.
//You need to make an informed choice, the following URL is useful
//http://gafferongames.com/game-physics/fix-your-timestep/
// Processing Collisions
// --------------------------------------------------------------------------------
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();
CCSprite *sA = (CCSprite *) bodyA->GetUserData();
CCSprite *sB = (CCSprite *) bodyB->GetUserData();
if(contact.fixtureA == _topFixture){
NSLog(@"1 -> Object with Tag %d touched top line (Object A is TOP)!",sB.tag);
toDestroy.push_back(bodyB); // Löschen
NSLog(@"1 <-> Bullet in Queue!");}
if(contact.fixtureB == _topFixture){NSLog(@"Object with Tag %d touched top line (Object B is TOP)!",sA.tag);}
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
NSLog(@"Collision between a:%d and b:%d detected!",spriteA.tag,spriteB.tag);
}
}
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();
[self removeChild:sprite cleanup:YES];
CCLOG(@"1 <- BulletSprite was Deleted!");
}
world->DestroyBody(body);
currBullets --; // Wieder abziehen
CCLOG(@"1 <- BulletBody was Deleted!");
}
//---------------------------------------------------------------------------------
int32 velocityIterations = 8;
int32 positionIterations = 1;
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);
//Iterate over the bodies in the physics world
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
Help required *g*