SpriteKit - Trying to implement a a 2.d.p timer which increments per 1 second. How can I do this?

StackOverflow https://stackoverflow.com/questions/22771098

  •  25-06-2023
  •  | 
  •  

Question

I've tried using NSTimer but cannot get my head round it. Any help would be much appreciated

Was it helpful?

Solution 2

You can do this with SKActions. For example...

SKAction *wait = [SKAction waitForDuration:1.0f];
SKAction *sequence = [SKAction sequence:@[[SKAction performSelector:@selector(methodToFire:) onTarget:self], wait]];
SKAction *repeat   = [SKAction repeatActionForever:sequence];
[self runAction:repeat]; 


- (void)methodToFire {
...
}

OTHER TIPS

If you're wanting to do something game logic related, it's pretty easy to rig the update method to do what you want.

-(void)update:(CFTimeInterval)currentTime
{
    /* Called before each frame is rendered */
    // Handle time delta.
    // If we drop below 60fps, we still want things to happen at the same realtime rate.

    CFTimeInterval timeSinceLast = currentTime - lastUpdateTimeInterval;
    lastUpdateTimeInterval = currentTime;

    [self updateWithTimeSinceLastUpdate:timeSinceLast];

}

- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast
{

    lastEventInterval += timeSinceLast;
    //this is the part that will function like a timer selector and run once a second.
    if (lastEventInterval > 1)
    {
        lastEventInterval = 0;
       //do whatever you want to do once a second here
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top