Adding touch interactivity
To add touch interactivity, we will use the touchesBegan
function, similar to how we have always used it in SpriteKit. Here, we get the location of the touch and the name of the sprite under the touch location. If the sprite name is jumpBtn
and the gameOver
Boolean is false
, then we call the heroJump
function in the gameScene
class. If gameOver
is true and the play button is clicked, then we call the startGame
function in the SceneKit class.
Add the function as follows to detect touches:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { /* Called when a touch begins */ for touch: AnyObject in touches { let location = touch.locationInNode(self) let _node:SKNode = self.nodeAtPoint(location); if(_gameScene.gameOver == false){ if(_node.name == "jumpBtn"){ _gameScene.heroJump() ...