Ondrej Rafaj
Ondrej Rafaj

Reputation: 4427

Calculating speed in a SpriteKit game

I am using SpriteKit and Kobald Kit (an open source addition) to create a tile based game with a character in the middle. At the moment I am calculating the speed value for the character when user uses a joystick component based on a CADisplayLink tick. The problem is that the character is faster on a higher frame rate, what would be the correct solution? I know I could simply use just the Y value from the joystick itself but I would than loose the nice acceleration / deceleration effect ...

Upvotes: 1

Views: 1401

Answers (1)

Greg
Greg

Reputation: 33650

I assume you're using the calculated velocity to adjust the position of elements in the game. You could calculate the delta time between the current tick and the last tick, and scale the speed according to this delta.

I do something like this in my SKScene's update method:

- (void) update:(NSTimeInterval) currentTime {
    NSTimeInterval delta = currentTime - self.lastTime;
    self.lastTime = currentTime;
    // use the time delta to determine how much of the velocity to add to the affected sprite
}

Upvotes: 2

Related Questions