在我的简单游戏中,我使用boundingBox
来挑选硬币和其他东西,但我需要使用Sprite的不规则区域检测(不含Alpha)。是否有替代boundingBox
?
这是代码:
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView:[touch view]];
CGPoint point = [[CCDirector sharedDirector] convertToGL:location];
curentPosition = point;
arrToDel = [[NSMutableArray alloc] initWithCapacity:0];
// Находим спрайт под касанием
if (CGRectContainsPoint([some boundingBox], curentPosition)) {
CCLOG(@"popal");
}
CCSprite *coin = nil;
for (Coins *coins in self.bugs) {
if (CGRectContainsPoint([coins boundingBox], curentPosition)) {
coin = coins; // нашли монету
}
}
if (coin != nil) {
NSMutableArray *checkList = [NSMutableArray arrayWithCapacity:0];
for (Coins *coins in self.bugs) {
if (CGRectIntersectsRect([coin boundingBoxInPixels], [coins boundingBox]) && coins != coin) {
[checkList addObject:coins];
}
}
int max = coin.zOrder;
for (Coins *b in checkList) {
if (b.zOrder > max)
max = b.zOrder;
}
if (max == coin.zOrder) {
[self removeChild:coin cleanup:YES];
podsciot++;
CCLOG(@"%i",podsciot);
[arrToDel addObject:coin];
for (Coins *coins in arrToDel) {
if (coins.type == kKey) {
coinsCount++;
CCLOG(@"SeriiZ --> %i", coinsCount);
}
[self.bugs removeObject:coin];
}
}
}
答案 0 :(得分:0)
如果您需要不规则的形状,我会亲自推荐Box2D和PhysicsEditor。它有一个轻微的学习曲线(Box2D),但在大多数用途中非常值得。
Box2D (although it comes with Cocos2D and has a template built in)
它不仅适用于物理模拟。如果您愿意,可以仅将其用于碰撞检测。 Ray Wenderlich的网站有一个很好的教程:
答案 1 :(得分:0)
我用CGPath
解决了这个问题。