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

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

问题描述

如何创建一个计时器,每两秒触发一次,将我在屏幕上的HUD上的分数增加一个?这是我对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];
    }
}


推荐答案

In Sprite Kit 不使用 NSTimer performSelector:afterDelay:或Grand Central Dispatch(GCD) ,即任何 dispatch _... 方法)因为这些计时方法忽略节点,场景或视图的暂停状态。此外,你不知道它们在游戏循环中的哪个位置被执行,这可能会导致各种问题,具体取决于你的代码实际做了什么。

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 更新:方法,不知道如何将其转发到节点或转发不方便的地方。

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

Swift 3

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

Swift 2

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

Aund重复运行代码:

Aund to run the code repeatedly:

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

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

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