Cocos2D - 检测碰撞

时间:2012-05-31 03:24:55

标签: objective-c ios cocos2d-iphone collision-detection

我是cocos2d的初学者,我面临着检测我的硬币碰撞的问题。 有时它有时会起作用。

所以基本上,我创造了一个游戏,用户(船)必须避开障碍物并在途中收集硬币。障碍物的碰撞效果很好但不适用于硬币。 我想也许创造许多硬币的循环是问题,但我不确定。

有人可以帮忙吗?

我的代码:

- (void)update:(ccTime)dt{
double curTime = CACurrentMediaTime();
if (curTime > _nextBridgeSpawn) {

    float randSecs = [self randomValueBetween:3.0 andValue:5.0];
    _nextBridgeSpawn = randSecs + curTime;

    float randX = [self randomValueBetween:50 andValue:500];
    float randDuration = [self randomValueBetween:8.0 andValue:10.0];

    CCSprite *bridge = [_bridge objectAtIndex:_nextBridge];

    _nextBridge++;

    if (_nextBridge >= _bridge.count) _nextBridge = 0;

    [bridge stopAllActions];
    bridge.position = ccp(winSize.width/2, winSize.height);
    bridge.visible = YES;

    [bridge runAction:[CCSequence actions:
                         [CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height)],
                         [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)],
                         nil]];

这是我申报硬币的地方(从更新方法继续)

    int randCoin = [self randomValueBetween:0 andValue:5];
    _coin = [[CCArray alloc] initWithCapacity:randCoin];
    for(int i = 0; i < randCoin; ++i) {
        coin = [CCSprite spriteWithFile:@"coin.png"];
        coin.visible = NO;
        [self addChild:coin];
        [_coin addObject:coin];
    }

    float randCoinX = [self randomValueBetween:winSize.width/5 andValue:winSize.width - (border.contentSize.width *2)];
    float randCoinY = [self randomValueBetween:100 andValue:700];
    float randCoinPlace = [self randomValueBetween:30 andValue:60];
    for (int i = 0; i < _coin.count; ++i) {
        CCSprite *coin2 = [_coin objectAtIndex:i];
        coin2.position = ccp(randCoinX, (bridge.position.y + randCoinY) + (randCoinPlace *i));
        coin2.visible = YES;
        [coin2 runAction:[CCSequence actions:
                          [CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height-2000)],
                          [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)],
                          nil]];  
    }

}

这是检查碰撞(也在更新方法中)

for (CCSprite *bridge in _bridge) {        
    if (!bridge.visible) continue;

if (CGRectIntersectsRect(ship.boundingBox, bridge.boundingBox)){
            bridge.visible = NO;
            [ship runAction:[CCBlink actionWithDuration:1.0 blinks:5]];         

        }
    }
}

//this is the collision for coins which only work at times
    for (CCSprite *coin2 in _coin) { 

        if (!coin2.visible) continue;

        if (CGRectIntersectsRect(ship.boundingBox, coin2.boundingBox)) {
            NSLog(@"Coin collected");
            coin2.visible = NO;
        }

    }

}

谢谢。

3 个答案:

答案 0 :(得分:0)

最后一段代码中的手机是什么?你在那里检查它是否与一个硬币相交,但我在其他地方看不到它。另一个,boundingBox只有在你的对象具有相同的父对象时才会起作用,因为它返回“local”rect,并且与它的父对象相关

另一点是,如果物体的移动速度太快,它们可能会在嘀嗒时间内相互传递。我的意思是,在碰撞之前会调用一个更新,因此它不会检测到它,并且在碰撞完成后将调用下一个更新,因此它也不会检测到它。

答案 1 :(得分:0)

为什么你不检查距离?

 float dist = ccpDistance(ship.position, coin2.position); 
 float allowedDist = ship.contentSize.width*0.5 + coin2.contentSize.width*0.5;
 if (dist<allowedDist){
   NSLog(@"Collision detected");
 }

如果您的精灵不在同一层上,则这些图层应至少具有相同的位置。

答案 2 :(得分:0)

我通过在init中创建一个计时器来解决我的问题,然后每隔10秒调用一次以创建我的硬币。

我的代码:

-(void) createCoins:(ccTime)delta{ 

CGSize winSize = [CCDirector sharedDirector].winSize;

int randCoin = [self randomValueBetween:0 andValue:10];
_coin = [[CCArray alloc] initWithCapacity:randCoin];
for(int i = 0; i < randCoin; i++) {
    coin = [CCSprite spriteWithFile:@"coin30p.png"];
    coin.visible = NO;
    [self addChild:coin];
    [_coin addObject:coin];
}
float randCoinX = [self randomValueBetween:winSize.width/5 andValue:winSize.width - (border.contentSize.width *2)];
float randCoinY = [self randomValueBetween:100 andValue:700];
float randCoinPlace = [self randomValueBetween:30 andValue:60];
for (int i = 0; i < _coin.count; i++) {
    CCSprite *coin2 = [_coin objectAtIndex:i];
    coin2.position = ccp(randCoinX, (winSize.height + randCoinY) + (randCoinPlace *i));
    coin2.visible = YES;

    [coin2 runAction:[CCSequence actions:
                      [CCMoveBy actionWithDuration:8 position:ccp(0, -winSize.height-2000)],
                      [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)],
                      nil]];  
}

}

在更新方法中:

for (CCSprite *coins in _coin){
    if (!coins.visible) continue;
    if (CGRectIntersectsRect(phone.boundingBox, coins.boundingBox)) {
        NSLog(@"Coin collected");
        coins.visible = NO;
    }

}

谢谢大家的帮助(: