开发者

How to implement powerUps and other game altering objects in objective-C or cocos2d

Ok, so I have these powerups that I want to slow/speed up the movement of the other objects in the game for a few seconds.

I have an array of objects that I have a variable called spawnInterval that gets faster and faster as the game progresses, making the ame get harder after a few mins.

But I can't really grasp how to make it so the character in the game will react differently to different objects as in when the fastPowerUp is hit by the character sprite, the spawn interval doesn't change.

And vice versa with the slowPowerUp.

the code I have at the moment is this in a move sequence method that gets called in an update method:

-

(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;
}

I really just don't know where to go from here... Should I make the powerUps a different class? If so, how would I implement something like this? I really hate trying to ask for someone to solve my question, but I really just can't rack my brain around this and I'm rather new... if it were explained to me, then I know I would be able to implement it in future games on my own...

Thanks in advance, and let me know if more information is needed...


I'd do something like

in the .h file

float speedModifier;
-(void)resetPowerUp;

in the .m

-(void)resetPowerUp
{
    speedModifier = 1;
}

wherever you are initializing the level

[self resetPowerUp];

upon collision with powerup:

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

then wherever you are moving whatever it is which speed should be effected by the powerup mode, multiply the speed of the animation (or divide the duration it takes for it to get wherever it's going) by speedModified

hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜