_ball = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
_ball.position = ccp(300, 300);
[self addChild:_ball];
_ball1 = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
_ball1.position = ccp(300, 300);
[self addChild:_ball1];
if (_ball1.position.x == _ball.position.x && _ball1.position.y == _ball.position.y) {
NSLog(@"Stop");
}
如果球是触球 1 不工作 这里面有什么问题吗?
最佳答案
使用CGRectIntersectsRect
确定 Sprite 是否重叠:
if (CGRectIntersectsRect([_ball1 boundingBox], [_ball2 boundingBox])) {
NSLog(@"Stop");
}
当然,这只是一个矩形的交集,可能不适用于圆形的物体,可以引用这篇webpage中的思路。 .
关于ios - 比较两个 CCSprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17831507/