【问题标题】:cocos2d Children sprite collision detection with parent spritecocos2d 子精灵与父精灵的碰撞检测
【发布时间】:2012-07-05 07:51:57
【问题描述】:

你好 stackoverflow 社区!

如何在 cocos2d 中检测子精灵与父精灵的碰撞?

目前我有这样的代码:

    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];

现在如何检测“jumper”和“plat”之间的碰撞?

感谢您的帮助!

【问题讨论】:

  • 首先,为什么你初始化你的平台两次?你会在这里得到内存泄漏。第二个你可以将比较精灵的矩形坐标转换为世界坐标,然后比较它们

标签: cocos2d-iphone parent collision-detection sprite


【解决方案1】:

通常你可以像这样检查碰撞:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2012-03-04
    • 2019-11-05
    相关资源
    最近更新 更多