我刚刚开始学习一些cocos2d,这个问题困扰了我很长一段时间。基本上我想要做的是通过使用ccTouchBegan
和ccTouchMoved
检查触摸是否落在精灵边界框上来移动图层中的精灵。
一切正常,直到我移动了图层,其中包含许多其他精灵,并且还比屏幕尺寸大。移动图层后,精灵的边界框与精灵图像显示的位置不同。以前有没有人经历过类似的问题?
答案 0 :(得分:2)
精灵的boundingBox
总是相对于精灵的父坐标系。如果您移动,旋转或缩放父级,则子级仍将具有相同的boundingBox
。您可以将其转换为另一个坐标系。如果仅移动了父级(未旋转或缩放),则只需更改origin
的{{1}}即可转换为世界坐标系:
boundingBox
如果缩放父级,则子级boundingBox的大小会相应更改。如果父项被旋转,它会变得非常复杂,因为子项的boundingBox的比例和宽高比都可以改变。如果你要做的就是测试boundigBox中是否发生了触摸,将触摸位置转换为孩子的父坐标系:
CGRect boundingBox = child.boundingBox;
boundingBox.origin = [child.parent convertToWorldSpace:boundingBox.origin];
NSLog(@"%@", NSStringFromCGRect(boundingBox));
现在CGPoint touchLocation = [child.parent convertToNodeSpace:touchWorldLocation]
和child.boundingBox
位于相同的坐标系中。
答案 1 :(得分:0)
使用boundingBox的数组。
CGRect boundingBoxuser = user.boundingBox;
for (CCSprite *spritecoinleft in Arraycoinleft)
{
CGRect boundingBoxcoinleft = spritecoinleft.boundingBox;
if((CGRectIntersectsRect(boundingBoxcoinleft,boundingBoxuser)))
{
CCLOG(@"hi....!!");
}
}