How to create a timer in Spritekit?

Ive figured out how to make a timer in a single view application, but not Spritekit. When I use the following code, I get 2 errors (listed below). Can anyone help me with this? Thanks Jack.

Timer.

if(!_scorelabel) {
    _scorelabel = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"];

    _scorelabel.fontSize = 200;
    _scorelabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    _scorelabel.fontColor = [SKColor colorWithHue: 0 saturation: 0 brightness: 1 alpha: .5];
    [self addChild:_scorelabel];
}
_scorelabel = [NSTimer scheduledTimerWithTimeInterval: 1 target:self selector:@selector(Timercount)userInfo: nil repeats: YES];

Mistakes

Incompatible pointer types assigned to 'SKLabelNode*' from 'NSTimer*'
Undeclared selector 'Timercount'
+4
source share
2 answers

In Swift, you can use:

Do not use in the Sprite Kit NSTimer, GCD or performSelector:afterDelay:because these synchronization methods ignore the state of the node, scene, or paused view. In addition, you do not know at what point in the game cycle they are being executed, which can cause a lot of problems depending on what your code actually does.

var actionwait = SKAction.waitForDuration(0.5)
        var timesecond = Int()
        var actionrun = SKAction.runBlock({
                timescore++
                timesecond++
                if timesecond == 60 {timesecond = 0}
                scoreLabel.text = "Score Time: \(timescore/60):\(timesecond)"
            })

        scoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
+4

NSTimer, , SKLabelNode. , :

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(Timercount) userInfo:nil repeats:YES];

, Timercount, .

0

Source: https://habr.com/ru/post/1538660/


All Articles