2017-09-25 48 views
0

我想輕掃手勢添加到從主場景繼承場景(GameScene)respondToSwipeGesture在很多場景

extension GameScene { 

    class Scene1: GameScene { 

     override func didMove(to view: SKView) { 

      let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) 
      swipeRight.direction = .right 
      view.addGestureRecognizer(swipeRight) 

      let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) 
      swipeDown.direction = UISwipeGestureRecognizerDirection.down 
      view.addGestureRecognizer(swipeDown) 

      let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) 
      swipeUp.direction = UISwipeGestureRecognizerDirection.up 
      view.addGestureRecognizer(swipeUp) 

      let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) 
      swipeLeft.direction = UISwipeGestureRecognizerDirection.left 
      view.addGestureRecognizer(swipeLeft) 
     } 
    } 
    class Scene2: GameScene { 

     override func didMove(to view: SKView) { 

      let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) 
      swipeRight.direction = .right 
      view.addGestureRecognizer(swipeRight) 

      let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) 
      swipeDown.direction = UISwipeGestureRecognizerDirection.down 
      view.addGestureRecognizer(swipeDown) 

      let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) 
      swipeUp.direction = UISwipeGestureRecognizerDirection.up 
      view.addGestureRecognizer(swipeUp) 

      let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) 
      swipeLeft.direction = UISwipeGestureRecognizerDirection.left 
      view.addGestureRecognizer(swipeLeft) 
     } 
    } 
} 

而且每次我必須不時在所有重新編寫代碼等及其他場景。也許,還有另一種出路。

回答

0

是的,如果你看了你的代碼,你會發現你永遠不會爲場景添加一個手勢,而是將它添加到視圖中。現在問自己,你還有什麼其他物體可以訪問視圖?答案是視圖控制器。這是你想要放置你的手勢代碼的地方。

+0

我試過但我做不到。 – Proffan

+0

是什麼?這是沒有意義的 – Knight0fDragon

+0

var scene = GameScene(size:CGSize(width:1024,height:768)) func viewDidLoad(){ super.viewDidLoad() let view = self.view as! SKView let swipeDown = UISwipeGestureRecognizer(target:scene,action:#selector(scene.respondToSwipeGesture(gesture :) :)) swipeDown.direction = .down view.addGestureRecognizer(swipeRight) – Proffan