确定CCSprite的不规则区域

时间:2011-07-27 14:39:27

标签: iphone cocos2d-iphone

在我的简单游戏中,我使用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];

        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果您需要不规则的形状,我会亲自推荐Box2D和PhysicsEditor。它有一个轻微的学习曲线(Box2D),但在大多数用途中非常值得。

Box2D (although it comes with Cocos2D and has a template built in)

PhysicsEditor (not free, but I recommend it as the program is very easy to use and the developer is a kind man)

它不仅适用于物理模拟。如果您愿意,可以仅将其用于碰撞检测。 Ray Wenderlich的网站有一个很好的教程:

Box2D For Just Collision Detection

答案 1 :(得分:0)

我用CGPath解决了这个问题。

本教程帮助了我http://bobueland.com/cocos2d/?p=134