Skip to content

Instantly share code, notes, and snippets.

@macshome
Created January 9, 2014 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macshome/8337984 to your computer and use it in GitHub Desktop.
Save macshome/8337984 to your computer and use it in GitHub Desktop.
In my asteroids-style game I was looking for a way to limit the speed of the ship so that it didn't get too fast or outrun the missiles it fires. I call this with the update: method on the scene and it just checks the dx and dy values to make sure they are all inside the speed I want to set. In real code I would use a static or value loaded from…
// Speed limiter
- (void)shipSpeedLimit {
// Check the x velocity
if (self.ship.physicsBody.velocity.dx > 500) {
self.ship.physicsBody.velocity = CGVectorMake(500, self.ship.physicsBody.velocity.dy);
} else if (self.ship.physicsBody.velocity.dx < -500) {
self.ship.physicsBody.velocity = CGVectorMake(-500, self.ship.physicsBody.velocity.dy);
}
// Check the y velocity
if (self.ship.physicsBody.velocity.dy > 500) {
self.ship.physicsBody.velocity = CGVectorMake(self.ship.physicsBody.velocity.dx, 500);
} else if (self.ship.physicsBody.velocity.dy < -500) {
self.ship.physicsBody.velocity = CGVectorMake(self.ship.physicsBody.velocity.dx, -500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment