cocos2d Children sprite collision detection with parent sprite

元气小坏坏 提交于 2019-12-11 04:49:13

问题


Hi stackoverflow community!

How do you detect a children sprite collision with a parent sprite in cocos2d?

Currently I have my codes like this:

    CGSize screenSize = [[CCDirector sharedDirector]winSize];

    parentJumper = [CCSprite spriteWithFile:@"inviBtn.png"];
    jumper = [CCSprite spriteWithFile:@"jumperRight.png"];

    plat = [[Platform alloc]init];

    plat = [Platform spriteWithFile:@"platformBlack.png"];

    plat.position = ccp(160,100);

    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0/60)];

    jumper.anchorPoint = ccp(0.5, 0);
    jumper.position = ccp(0, 20);
    parentJumper.position = ccp(screenSize.width/2, 0);

    [self addChild:plat];
    [self addChild:parentJumper];
    [parentJumper addChild:jumper];

Now how do I detect the collision between "jumper" & "plat"?

Thanks for your help!


回答1:


Usually you can check collision like this:

if(CGRectIntersectsRect([jumper boundingBox], [plat boundingBox])) {
  //Handle collision<br>
}


来源:https://stackoverflow.com/questions/11340024/cocos2d-children-sprite-collision-detection-with-parent-sprite

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