Bar Hopping

Now that the hero is animated, you can make him or her jump on command. You start by defining a few physics properties, such as a velocity and gravity. If you’re not into math, don’t worry. The calculations involve basic addition and subtraction. Simply change the hero’s velocity on every frame and during touch events, like this:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {    if self.onGround {        self.velocityY = -18.0        self.onGround = false    }}override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {    if self.velocityY < -9.0 {        self.velocityY = -9.0    }}override func update(currentTime: NSTimeInterval) {    //jump    self.velocityY += ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.