简体   繁体   English

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

[英]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. 我有一个对象数组,其中有一个名为spawnInterval的变量,随着游戏的进行,它会越来越快,这使得几分钟后目标变得越来越难。

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. 但是我无法真正掌握如何做到这一点,因此游戏中的角色会对不同对象的反应不同,就像当fastPowerUp被角色精灵击中时,生成间隔不会改变。

And vice versa with the slowPowerUp. 反之亦然,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? 我真的只是不知道从这里去哪里。。。我应该把powerUps换个类吗? 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 在.h文件中

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 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM