cocos2d v3 collision detection

纵然是瞬间 提交于 2019-12-02 10:43:11

问题


I'm trying to inspect collision collision of two bodies, but collision detection callbacks are not being fired. Here is my code:

1) My CCScene implements CCPhysicsCollisionDelegate protocol

2) I set collision delegate for physics

_physics = [CCPhysicsNode node];
_physics.gravity = PHYSICS_GRAVITY;
_physics.debugDraw = YES;
_physics.collisionDelegate = self;
[self addChild:_physics];

3) For each of two body I set a collision type

body1.collisionType       = @"body1";
body2.collisionType       = @"body2";

4) That's it, when these two bodies collide none of CCPhysicsCollisionDelegate callback methods is being called.

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA     typeB:(CCNode *)nodeB
{
    NSLog(@"HELLO");
    return YES;
}

Could you please help me with this? Have you been able to receive collision callbacks in cocos2d v3?

Thanks in advance


回答1:


In cocos2d v3 physics, collisionType eliminates the need to set integer bit masks to define the type of collision. The parameter name CCPhysicsCollisionDelegate methods must be the collisionTypes that you want to deal with yourself. So in your case , the collision callback method should be

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair body1:(CCNode *)nodeA     body2:(CCNode *)nodeB
{
    NSLog(@"HELLO");
    return YES;
}

By default everything collides in cocos2d, but if you set the collisionGroup of two bodies to be the same then they wouldn't collide.



来源:https://stackoverflow.com/questions/22680338/cocos2d-v3-collision-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!