Game Development Reference
In-Depth Information
b2Sprite * spriteA = (b2Sprite *) bodyA->GetUserData();
b2Sprite * spriteB = (b2Sprite *) bodyB->GetUserData();
if (spriteA && spriteB) {
//track collision between balls and pockets
if (spriteA->getType() == kSpritePocket) {
spriteB->setVisible(false);
} else if (spriteB->getType() == kSpritePocket) {
spriteA->setVisible(false);
} else if (spriteA->getType() == kSpriteBall &&
spriteB->getType() == kSpriteBall) {
if (spriteA->mag() > 10 || spriteB->mag() > 10)
{
SimpleAudioEngine::getInstance()->playEffect("ball.wav");
}
} else if ((spriteA->getType() == kSpriteBall &&
spriteB->getType() == kSpritePlayer) ||
(spriteB->getType() == kSpriteBall &&
spriteA->getType() == kSpritePlayer)) {
if (spriteA->mag() > 10 || spriteB->mag() > 10)
{
SimpleAudioEngine::getInstance()->playEffect("ball.wav");
}
}
}
}
You can see now how important the
userData
property is. We can quickly access
sprites attached to the bodies listed in the
b2Contact
object through the
userData
property.
Besides that, all our sprites have a
_type
property that behaves like identifying tags in
our logic. Note that you could certainly use the Cocos2d-x tags for that, but I find that at
times, if you can combine the
Sprite
tags with their
_type
value, you may produce in-
teresting sorting logic.