OStack程序员社区-中国程序员成长平台

标题: ios - Sprite Kit 在失去生命时暂停游戏 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:02
标题: ios - Sprite Kit 在失去生命时暂停游戏

我想不通。我有一个游戏,玩家必须在浆果从天上掉下来时接住它们。当玩家错过一个时,我希望屏幕闪烁红色,浆果停止掉落,让游戏暂停 3 秒然后继续。

@interface SpriteMyScene : SKScene{
    SKAction *sceneUnPaused;
    SKAction *scenePaused;   
}

2种暂停游戏的方法。

-(SKAction*)unpaused {
    sceneUnPaused = [SKAction runBlock:^{
        self.scene.view.paused = NO;
    }];
    return sceneUnPaused;}

    -(SKAction*)paused{
               scenePaused = [SKAction runBlock:^{
        self.scene.view.paused = YES;
    }];
    return scenePaused;
}

创建浆果的方法及其 Action 。

- (void)addBerry {
...
...
// Create the actions
//For each individual berry
SKAction * actionMove = ...
SKAction * gameWon = ...
SKAction * actionMoveDone = ...

SKAction *wait3Seconds = [SKAction waitForDuration:3];
SKAction * loseAction = [SKAction runBlock:^{
    [self subtractLives];
    NSLog(@"Lost a life");
    [self vibrate];
    [self runAction:scenePaused];
    [self runAction:wait3Seconds];
    [berry removeFromParent];
    [self runAction:sceneUnPaused];

    if (_playerLives == 0){
        [[SoundManager sharedManager] stopMusic];
        SKTransition *reveal = [SKTransition fadeWithDuration:1.0];
        SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size andScore:_berriesKilled];
        [self.view presentScene:gameOverScene transition: reveal];}}];

SKAction *berrie = [SKAction runBlock:^{
        [berry runAction:actionMove];}];

[berry runAction:[SKAction sequence[berrie, gameWon, actionMove,  loseAction, actionMoveDone]]];

}

我不知道你们是否有必要帮助我弄清楚为什么它不起作用,但这里是决定我何时生成浆果的更新方法。

- (void)updateWithTimeSinceLastUpdateCFTimeInterval)timeSinceLast {

self.lastSpawnTimeInterval += timeSinceLast;

if (self.lastSpawnTimeInterval > [self determineAmountOfBerries]) {
self.lastSpawnTimeInterval = 0;
[self addBerry];

}

}



Best Answer-推荐答案


当玩家错过一个浆果时,只需这样做:

[self.scene.view setPaused:YES];

启动一个 NSTimer,在 3 秒后执行以下操作:

[berry removeFromParent];
[self.scene.view setPaused:NO];

关于ios - Sprite Kit 在失去生命时暂停游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21865658/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4