Swift Game Development(Third Edition)
上QQ阅读APP看书,第一时间看更新

Listening for touches in GameScene

The SKScene class (that GameScene inherits from) includes handy functions that we can use to monitor touch input. Follow these steps to wire up the GameScene class:

  1. In GameScene.swift, in the touchesBegan function, add this code at the very bottom to start the Player flapping when the user touches the screen:
    player.startFlapping() 
  2. After touchesBegan, create two new functions in the GameScene class. These functions stop the flapping when the user lifts his or her finger from the screen, or when an iOS notification interrupts the touch:
            override func touchesEnded(_ touches: Set<UITouch>, 
                with event: UIEvent?) { 
    player.stopFlapping() 
            } 
    
            override func touchesCancelled(_ touches: Set<UITouch>,  
                with event: UIEvent?) { 
    player.stopFlapping() 
            }