SpriteKit - 创建一个计时器 [英] SpriteKit - Creating a timer

查看:23
本文介绍了SpriteKit - 创建一个计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个每两秒触发一次的计时器,以便在我屏幕上的 HUD 上将分数增加 1?这是我的 HUD 代码:

How can I create a timer that fires every two seconds that will increment the score by one on a HUD I have on my screen? This is the code I have for the HUD:

    @implementation MyScene
{
    int counter;
    BOOL updateLabel;
    SKLabelNode *counterLabel;
}

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        counter = 0;

        updateLabel = false;

        counterLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        counterLabel.name = @"myCounterLabel";
        counterLabel.text = @"0";
        counterLabel.fontSize = 20;
        counterLabel.fontColor = [SKColor yellowColor];
        counterLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
        counterLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBottom;
        counterLabel.position = CGPointMake(50,50); // change x,y to location you want
        counterLabel.zPosition = 900;
        [self addChild: counterLabel];
    }
}

推荐答案

在 Sprite Kit 请勿使用 NSTimer, performSelector:afterDelay:或 Grand Central Dispatch(GCD,即任何 dispatch_... 方法),因为这些计时方法会忽略节点、场景或视图的 paused 状态.此外,您不知道它们是在游戏循环的哪个点执行的,这可能会导致各种问题,具体取决于您的代码实际执行的操作.

In Sprite Kit do not use NSTimer, performSelector:afterDelay: or Grand Central Dispatch (GCD, ie any dispatch_... method) because these timing methods ignore a node's, scene's or the view's paused state. Moreover you do not know at which point in the game loop they are executed which can cause a variety of issues depending on what your code actually does.

在 Sprite Kit 中执行基于时间的事情的唯一两种认可的方法是使用 SKScene update: 方法和使用传入的 currentTime 参数来跟踪时间.

The only two sanctioned ways to perform something time-based in Sprite Kit is to either use the SKScene update: method and using the passed-in currentTime parameter to keep track of time.

或者更常见的是,您只使用以等待操作开头的操作序列:

Or more commonly you would just use an action sequence that starts with a wait action:

id wait = [SKAction waitForDuration:2.5];
id run = [SKAction runBlock:^{
    // your code here ...
}];
[node runAction:[SKAction sequence:@[wait, run]]];

并重复运行代码:

[node runAction:[SKAction repeatActionForever:[SKAction sequence:@[wait, run]]]];

或者,您也可以使用 performSelector:onTarget: 而不是 runBlock: 或者如果您需要模仿,也可以使用 customActionWithDuration:actionBlock:SKScene update: 方法,不知道怎么转发到节点或者哪里不方便转发.

Alternatively you can also use performSelector:onTarget: instead of runBlock: or perhaps use a customActionWithDuration:actionBlock: if you need to mimick the SKScene update: method and don't know how to forward it to the node or where forwarding would be inconvenient.

参见 SKAction 参考 了解详情.

更新:使用 Swift 的代码示例

UPDATE: Code examples using Swift

斯威夫特 5

 run(SKAction.repeatForever(SKAction.sequence([
     SKAction.run( /*code block or a func name to call*/ ),
     SKAction.wait(forDuration: 2.5)
     ])))

斯威夫特 3

let wait = SKAction.wait(forDuration:2.5)
let action = SKAction.run {
    // your code here ...
}
run(SKAction.sequence([wait,action]))

斯威夫特 2

let wait = SKAction.waitForDuration(2.5)
let run = SKAction.runBlock {
    // your code here ...
}
runAction(SKAction.sequence([wait, run]))

并重复运行代码:

runAction(SKAction.repeatActionForever(SKAction.sequence([wait, run])))

这篇关于SpriteKit - 创建一个计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆