Created
January 9, 2014 17:13
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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