2016-12-12 67 views
1

scenekit人物移動我剛剛完成我的scenekit遊戲,在其中我有一個移動的特性而開始與單個磁帶移動gesture.I希望當按下的按鈕移動到,所以我創建播放/暫停按鈕在skscene class.How我讓我的角色只有當我按下播放按鈕時纔會移動? 由於這種圖像enter image description here 這裏是我的代碼如何控制與SkSpriteNode

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

    for touch in touches { 
     let location = touch.location(in: self) 
     let _node:SKNode = self.atPoint(location) 
     if(_node.name == "Play Button"){ 
         parentView?.startGame() 

     } 
      if self.pauseNode.contains(location) { 
       if !self.isPaused { 
        self.pauseNode.texture = SKTexture(imageNamed: "Pause Button") 

       } 
       else { 
        self.pauseNode.texture = SKTexture(imageNamed: "Play Button") 

       } 

       self.isPaused = !self.isPaused 



     } 
    } 

功能在LevelOneViewController viewDidLoad中()

func move(){ 
    Character.idle() 
    let startTap = UITapGestureRecognizer(target: self, action: #selector(LevelOneViewController.handleStartTap(_:))) 
    scnView.addGestureRecognizer(startTap) 

    let stopTap = UITapGestureRecognizer(target: self, action: #selector(LevelOneViewController.handleStopTap(_:))) 
    scnView.addGestureRecognizer(stopTap) 
    stopTap.numberOfTapsRequired = 2 
    } 
    move() 
} 

回答

2

移動角色你需要創建一個覆蓋SKSceneSKSpriteNode代表您的播放按鈕,添加一個手勢識別器添加到您的節點,然後更新您的角色。然後,您將此控件場景設置爲您的SCNView的疊加層。

一些示例僞代碼,您需要填寫詳細信息:

class GameControlsScene:SKScene { 

    var character:Character 
    var playButton:SKSpriteNode 
    var playButtonGestureRecognizer:UIGestureRecognizer 

    override init(character:Character) { 
    self.character = character 
    super.init() 
    } 

    func didMove(to:SKView) { 
    // add an SKSpriteNode for the play button 
    self.playButton = SKSpriteNode(imageNamed:"playButton") 
    self.playButton.position = CGPointMake(<<w>>, <<h>>) 
    self.playButtonGestureRecognizer = // add a UIGestureRecognizer with selector as handlePlayButtonTap 
    self.view.addGestureRecognizer(self.playButtonGestureRecognizer) 
    } 

    // gesture recognizer to handle play button tap 
    func handlePlayButtonTap() { 
    //in here move your character  
    } 
} 

現在,您可以設置您的SCNView設置疊加。

var controlScene:GameControlScene(<<character>> 
scnView.overlaySKScene = controlScene 

請注意,您也可以使Xcode的SKScene.sks)文件並加載它,而不是使一個編程的,但加入了手勢識別,掛鉤起來,以你的性格中的其餘的細節3D場景並將其設置爲疊加是相同的。

隨着.sks文件,this should help