如何在objective-C或cocos2d中实现powerUps和其他游戏改变对象

时间:2011-02-15 17:54:10

标签: iphone objective-c cocos2d-iphone

好的,所以我有这些通电,我想减慢/加快游戏中其他物体的移动几秒钟。

我有一个对象数组,我有一个名为spawnInterval的变量,随着游戏的进行变得越来越快,几分钟之后就变得越来越难了。

但是我无法真正掌握如何制作游戏中的角色会对不同的物体产生不同的反应,就像快速动作能力被角色精灵击中时一样,产生的间隔不会改变。

反之亦然,使用slowPowerUp。

我目前的代码是一个在更新方法中调用的移动序列方法:

-

(void) updateObstacles:(ccTime)delta{

    for (int i = 0; i < 20; i++) {
        //int randomizer = CCRANDOM_0_1() * [obstacles count];
        //NSLog(@"randomizer: %i",randomizer);
        CCSprite* randomObject = [obstacles randomObject];
        currentObject = [obstacles indexOfObject:randomObject];

            if ([randomObject numberOfRunningActions] == 0) {
                [self runObstacleMoveSequence:randomObject withTimer:delta];

                break;
        }
    }
}

-(void) runObstacleMoveSequence:(CCSprite *)object withTimer:(ccTime)delta{
    static int time;
    //Slowly increase object speed
    numObstaclesMoved++;
    if (!slowPowerUp && !fastPowerUp) {
        time += delta;
        if (numObstaclesMoved % 17 == 0 && obstacleMoveDuration > 2.0f) {
            obstacleMoveDuration -= 0.2f;
            if (spawnInterval > 0.1f) {
                [self unschedule:@selector(updateObstacles:)];
                [self schedule:@selector(updateObstacles:) interval:spawnInterval];
                spawnInterval-=0.1f;
                NSLog(@"interval: %f",spawnInterval);
            }
        }
    }else if (slowPowerUp && !fastPowerUp) {
        if (numObstaclesMoved % 17 == 0 && obstacleMoveDuration > 2.0f) {
            obstacleMoveDuration += 3.0f;
            if (spawnInterval > 0.1f) {
                [self unschedule:@selector(updateObstacles:)];
                [self schedule:@selector(updateObstacles:) interval:spawnInterval];
                spawnInterval-=0.1f;
                NSLog(@"interval: %f",spawnInterval);
                if (time >= (delta + 3)) {
                    slowPowerUp = NO;
                    obstacleMoveDuration -= 3.0f;
                }

            }
        }
    }else if (!slowPowerUp && fastPowerUp) {
        if (numObstaclesMoved % 17 == 0 && obstacleMoveDuration > 2.0f) {
            obstacleMoveDuration -= 3.0f;
            if (spawnInterval > 0.1f) {
                [self unschedule:@selector(updateObstacles:)];
                [self schedule:@selector(updateObstacles:) interval:spawnInterval];
                spawnInterval-=0.1f;
                NSLog(@"interval: %f",spawnInterval);
                if (time >= (delta + 3)) {
                    fastPowerUp = NO;
                    obstacleMoveDuration += 3.0f;
                }
            }
        }
    }
    CGSize screenSize = [[CCDirector sharedDirector]winSize];
    CGPoint aboveScreenPosition = CGPointMake(object.position.x, screenSize.height - object.position.y);

    int rotations = (CCRANDOM_0_1()*3) * 360;
    float duration = (CCRANDOM_0_1()*5.0f) + 8.0f;
    CCMoveTo* move = [CCMoveTo actionWithDuration:obstacleMoveDuration position:aboveScreenPosition];
    CCRotateTo* rotate = [CCRotateBy actionWithDuration:duration angle:rotations];
    CCSpawn* moveRotate = [CCSpawn actions: move, rotate, nil];
    CCCallFuncN* call = [CCCallFuncN actionWithTarget:self selector:@selector(objectAboveScreen:)];
    CCSequence* sequence = [CCSequence actions:moveRotate, call, nil];
    [object runAction:sequence];
    if (time >= (delta + 3)) {
        fastPowerUp = NO;
    }

}


-(void) objectAboveScreen:(id) sender{
    //make sure sender is actually of the right class
    NSAssert([sender isKindOfClass:[CCSprite class]], @"sender is not a CCSprite!");
    CCSprite* obstacle = (CCSprite*)sender;

    //move the back to the bottom of the screen
    CGPoint pos = obstacle.position;
    CGSize screenSize = [[CCDirector sharedDirector]winSize];
    pos.y = (-screenSize.height - [obstacle texture].contentSize.height);
    pos.x = CCRANDOM_0_1() * screenSize.width;
    obstacle.position = pos;
}

我真的不知道从哪里开始......我应该让powerUps成为另一个类吗?如果是这样,我将如何实现这样的东西?我真的很讨厌要求有人来解决我的问题,但我真的无法绕过这个问题,而且我很新......如果有人向我解释,那么我知道我能够实施它在我自己的未来游戏中......

提前致谢,如果需要更多信息,请告诉我......

1 个答案:

答案 0 :(得分:1)

我会做类似

的事情 <。>文件中的

float speedModifier;
-(void)resetPowerUp;
<。> <。>

-(void)resetPowerUp
{
    speedModifier = 1;
}

无论您在哪里初始化等级

[self resetPowerUp];

与通电相撞:

speedModifier = 2;
[self performSelector:@selector(resetPowerUp) withObject:nil afterDelay:5];

然后,无论你在哪里移动任何速度都应该通过加电模式实现,通过speedModified

将动画的速度乘以(或将它所需的持续时间除以它到达的任何地方)。

希望有所帮助