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
  •  | 
  •  

문제

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

도움이 되었습니까?

해결책 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 {
...
}

다른 팁

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
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top