开发者

Want label to display on ball collision in cocos2d and box2d application

开发者 https://www.devze.com 2023-02-26 02:57 出处:网络
I\'m using _world->step(dt,5,5) to detect the collision system. So col开发者_运维技巧lision is actually getting handled by box2d classes. Now I want to show score on balls collision depending on to wh

I'm using _world->step(dt,5,5) to detect the collision system. So col开发者_运维技巧lision is actually getting handled by box2d classes. Now I want to show score on balls collision depending on to which ball it has collide?

Can any one please help me out with this? If you guys want I can provide source code of tick function if required.

Your help is appreciated.

Thank you,

Ankita


First, subclass the C++ class b2ContactListener, for example:

class GamePhysicsContactListener : public b2ContactListener 
{
public:
    GamePhysicsContactListener();   
    ~GamePhysicsContactListener();  
    void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);

};

Implement the PostSolve method to do the checking on the two bodies that are in contact by using:

b2Body *bodyA = contact->GetFixtureA()->GetBody();
b2Body *bodyB = contact->GetFixtureB()->GetBody();

And finally, instantiate the listener by calling the line below in the method where you instantiate your b2World (most probably in your CCLayer init method):

_world->SetContactListener(new GamePhysicsContactListener());

p/s: instead of doing the contact checking logic in the GamePhysicsContactListener class, you can also make that class to accept a target and a selector to be invoked using NSInvocation. That would be a bit more complex though.

EDIT:

Here is the working implementation of GamePhysicsContactListener: https://gist.github.com/922824 (it's part of my private GamePhysics framework that bridges Cocos2D & Box2D classes)


create a fixture for each body and in tick function check for collision...

if((contact.fixtureA == fixture1 && contact.fixtureB == fixture2) ||
(contact.fixtureA == fixture2 && contact.fixtureB == fixture1))
{
//do something
}

Hope this helps!!!!


Here is a method I'm using to show a little animation upon collision. It could be pretty easily modified to show a label instead of an animation.

-(void) paintCollisionImage:(CGPoint) point{

CGSize screenSize = self.view.bounds.size;

CGRect myImageRect = CGRectMake((point.x -24), ((screenSize.height - point.y) -24), 48.0f, 48.0f);

NSArray *myImages = [NSArray arrayWithObjects: 
    [UIImage imageNamed:@"collideImage0.png"], 
    [UIImage imageNamed:@"collideImage1.png"], 
                     nil];
UIImageView *myAnimatedView = [UIImageView alloc]; 

[myAnimatedView initWithFrame:myImageRect]; 
myAnimatedView.animationImages = myImages; 
myAnimatedView.animationDuration = 0.25; // seconds 
myAnimatedView.animationRepeatCount = 1; // 0 = loops forever 

[myAnimatedView startAnimating]; 
[self.view addSubview:myAnimatedView]; 
[myAnimatedView release];

}

I call it like so:

[self paintCollisionImage:collisionPoint];


I was facing the same problem. I wanted to display a label when the player hits a power. For this I created a static function in CCScene which adds a label on the scene. And in the ContactListener I initialized the scene object in beginContact method and then called the scene function.

+(void)addPointLabel  
{  
    DataClass *d5=[DataClass getInstance];  
   [_label setString:@"+10"];        // Define label in init  
    _label.color = ccc3(0,255,0);  
    _label.position = ccp(d5.playerXPosition,d5.playerYPosition);  
    [self addChild:_label];  
    d5.lbl=_label;  
    id action1 = [CCMoveTo actionWithDuration:2 position:ccp(d5.playerXPosition,d5.playerYPosition+20)];  
    id action2 = [CCFadeOut actionWithDuration:1.0f];  
    [_label runAction: [CCSequence actions:action1,action2,  nil]];    
}  

Now call this method in your ContactListener class :

[GameScene addPointLabel];  
0

精彩评论

暂无评论...
验证码 换一张
取 消