【问题标题】:cocos2d sprite collision detection boundingboxcocos2d sprite 碰撞检测boundingbox
【发布时间】:2011-08-14 21:23:21
【问题描述】:

我有 2 个精灵。我使用边界框来检查与 CGRectIntersectsRect 的碰撞。但它不起作用。 HBBall 和 HBpaddle 都有一个名为 image 的 CCSprite。

初始化:

    ball = [[HBBall alloc] init];
    ball.position = ccp(150, 50);
    [self addChild:ball];
    [update addObject:ball];

    paddle1 = [[HBPaddle alloc] init];
    paddle1.position = ccp(50, 160);
    [self addChild:paddle1];

更新:

if (CGRectIntersectsRect([paddle1.image boundingBox], [ball.image boundingBox])) 
    CCLOG(@"ball hit paddle");

CGRectIntersectsRect 总是返回 true。有人有想法吗?

【问题讨论】:

    标签: iphone cocos2d-iphone collision-detection


    【解决方案1】:

    你不能直接传递边界框,因为它是相对于精灵的。您必须像这样传递绝对 CGRect 边界框:

    s = CCsprite
    s.anchorPoint = ccp(0, 0);    
    CGRect absoluteBox = CGRectMake(s.position.x, s.position.y, [s boundingBox].size.width, [s boundingBox].size.height);
    

    进行必要的调整!

    希望能帮上忙!

    【讨论】:

    • 一个小点,但是s.position.x和s.position.y不是指精灵的中心吗?要获得精灵的正确矩形,我认为您应该使用: CGRect absoluteBox = CGRectMake([s boundingBox].origin.x, [s boundingBox].origin.y, [s boundingBox].size.width, [s boundingBox ].size.height); HTH
    【解决方案2】:

    http://www.iphonedevsdk.com/forum/iphone-sdk-game-development/17082-cocos2d-collision-detection-between-sprites.html ?你用谷歌搜索过吗?这似乎是 cocos2d 框架中一个非常基本的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 2015-04-08
      • 1970-01-01
      • 2014-01-09
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多