我使用cocos2d v3创建简单的游戏,如flappy bird。 接口类:
@interface GameScene : CCScene<CCPhysicsCollisionDelegate>
// -----------------------------------------------------------------------
+ (GameScene *)scene;
- (id)init;
// -----------------------------------------------------------------------
@end
在.m文件中:
_physicsWorld =[CCPhysicsNode node];
_physicsWorld.gravity=ccp(0, 0);
_physicsWorld.collisionDelegate=self;
_physicsWorld.debugDraw =YES;
[self addChild:_physicsWorld];
.........
bird.physicsBody=[CCPhysicsBody bodyWithRect:(CGRect){CGPointZero,bird.contentSize} cornerRadius:0];
bird.physicsBody.collisionGroup=@"birdGroup";
bird.physicsBody.collisionType=@"birdCollision";
[_physicsWorld addChild:bird];
........
tubeBody.physicsBody.collisionGroup =@"tubeGroup";
tubeBody.physicsBody.collisionType=@"tubeCollision";
tubeBody.physicsBody.type=CCPhysicsBodyTypeStatic;
[_physicsWorld addChild:tubeBody];
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA typeB:(CCNode *)nodeB{
[self endGame];
NSLog(@"end game");
return YES;
}
为什么物理碰撞开始的方法从不打电话?
答案 0 :(得分:2)
更改
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA typeB:(CCNode *)nodeB{
要:
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair tubeCollision:(CCNode *)nodeA birdCollision:(CCNode *)nodeB{
你有碰撞委托的示例代码,所以你必须使用nodeA作为管子,将nodeB改为你的代码。