void MyContactListener::BeginContact(b2Contact* contact) { b2Body * bodyA = contact->GetFixtureA()->GetBody(); b2Body * bodyB = contact->GetFixtureB()->GetBody(); b2Sprite * spriteA = (b2Sprite *) bodyA->GetUserData(); b2Sprite * spriteB = (b2Sprite *) bodyB->GetUserData(); if (spriteA && spriteB) { Gate *gate = NULL; Ball *b = NULL; if (kSpriteFootballer == spriteA->getSpriteType() || kSpriteFootballer == spriteB->getSpriteType()) { SimpleAudioEngine::sharedEngine()->playEffect(BALL_SOUND); return; } if (kSpriteGate == spriteA->getSpriteType()) { gate = (Gate*)spriteA; b = (Ball*)spriteB; } else if (kSpriteGate == spriteB->getSpriteType()) { gate = (Gate*)spriteB; b = (Ball*)spriteA; } if(NULL != gate){ if (NULL != b) { if (gate->getType() == kRight && b->getPosition().x > (gate->getPositionX() - gate->boundingBox().size.width / 2)) { gate->setHoldsBall(true); } else if (gate->getType() == kLeft && b->getPosition().x < (gate->getPositionX() + gate->boundingBox().size.width / 2)) { gate->setHoldsBall(true); } } } } }