iphone - cocos2d CCSprite 碰撞问题

标签 iphone objective-c ios cocos2d-iphone ccsprite

[我有 2 个对象,它们都是 CCSprite 的子类。 他们每个人都有一个CCSprite变量,实际上代表 Sprite 图像

例子:

@interface Player : CCSprite
{
    CCSprite *sprite;
}

@property (nonatomic, retain) CCSprite *sprite;

我正在尝试检测两者的碰撞,但是当我尝试时:

- (void)detectCollision:(id)sender
{    
    for (Player *tempPlayer in self.playersArray) {        
        if (CGRectIntersectsRect([tempPlayer boundingBox], [mainPlayer boundingBox])) {
            //Collision
        }
    }
}

当我尝试时,它无法识别任何碰撞:

- (void)detectCollision:(id)sender
{    
    for (Player *tempPlayer in self.playersArray) {        
        if (CGRectIntersectsRect([tempPlayer.sprite boundingBox], [mainPlayer.sprite boundingBox])) {
            //Collision
        }
    }
}

当两个对象都显示在屏幕上时,即使尚未发生碰撞,它也会检测到碰撞。

编辑: 忘记将 boundingBox 添加到对象中...

谢谢

最佳答案

tempPlayer.sprite 和 mainPlayer.sprite 边界框将代表它们自己的 super View 中的矩形,这也是一个 Sprite ,而不是世界。例如,如果 mainPlayer 的 rect 是 (150, 200, 100, 100),并且它里面的 sprite 有相同的 rect,那么它的 boundingBox 就是 (0, 0, 100, 100)。

我曾经遇到过这个问题,解决方法如下: x for tempPlayer.sprite in world coordinates = x for tempPlayer + x for tempPlayer.sprite.

y 也一样。

关于iphone - cocos2d CCSprite 碰撞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8727492/

相关文章:

html - 由于 View 宽度,移动设备会拉伸(stretch)屏幕的 div

iphone - 与 RestKit 映射对象和引用作斗争

ios - 如何找到 Bridging-Header.h 的路径 - Swift,Xcode

ios - 打开我的项目时 XCode 4.31 崩溃

iphone - 在自定义类的子类上使用 NSCoding

iphone - iOS Settings.bundle 本地化困难

objective-c - -Wunused-value 警告的奇怪行为

java - 在 Mac 上打印到点阵打印机

ios - collectionView 的默认值 :layout:referenceSizeForFooterInSection

ios - 如何从 URL 方案中检索参数值