Game Development Reference
In-Depth Information
property. Go ahead and add this code to the touchesBegan() method of the GameS-
cene just after the playerNode.physicsBody.dynamic property is set to true .
This modified touchesBegan() method is shown here:
override func touchesBegan(touches: NSSet, withEvent event:
UIEvent) {
if !playerNode!.physicsBody!.dynamic {
playerNode!.physicsBody!.dynamic = true
self.coreMotionManager.accelerometerUpdateInterval
= 0.3
self.coreMotionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(),
withHandler: {
(data: CMAccelerometerData!, error: NSError!) in
if let constVar = error {
println("There was an error")
}
else {
self.xAxisAcceleration
= CGFloat(data!.acceleration.x)
}
})
}
if impulseCount > 0 {
playerNode!.physicsBody!.applyImpulse(CGVectorMake(0.0,
40.0))
impulseCount--
}
}
Now it is time to do something with this information. At first using the update() meth-
od would seem like the logical location to put this data to use, but the update() method
is invoked before the render loop has evaluated all of the physics bodies in the scene. It is
possible that colliding with another node in the scene could have altered the player's velo-
city along the x-axis, and this change should take place prior to altering the player's velo-
city with the accelerometer. While this cannot happen in this game, because the player-
Node is the only dynamic volume in the game, it is good to be aware of.
Given that all the physics changes should be evaluated before the player's velocity on the
x-axis is modified, there is really only one place in the render that can process accelero-
Search WWH ::




Custom Search