2017-04-09 17 views
-1

這是我在點擊屏幕的左側和右側時使節點移動的方式,但如何在左側和右側單手指放置時將其分開右邊?用於兩個節點的Swift Multi Touch

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let location = (touches.first)?.location(in: self.view) 
     if (location?.x)! < (self.view?.bounds.size.width)!/2 { 
      player1.run(SKAction.moveTo(x: 0 + player1.size.width/2, duration: 0.1)) 
      player2.run(SKAction.moveTo(x: 0 + player1.size.width + player2.size.width/2, duration: 0.1)) 
     } else { 
      player1.run(SKAction.moveTo(x: self.frame.width - player1.size.width - player2.size.width/2, duration: 0.1)) 
      player2.run(SKAction.moveTo(x: self.frame.width - player1.size.width/2, duration: 0.1)) 
     } 
    } 

回答

0

要啓用遊戲的多點觸控,您應該使用:view.isMultipleTouchEnabled = trueHere你可以找到API參考

然後,如果你從SKS文件出示您的場景,通常設置好的了anchorPoint(0.5,0.5)所以與我的示例工作下面一定要在加入這一行你GameViewController

scene.anchorPoint = CGPoint.zero 

之前行view.presentScene(scene)anchorPoint設置爲0爲您的當前場景。

您還應該考慮「保護」您的操作不受多個呼叫的影響,尤其是當您從觸摸事件委託方法啓動這些操作時。這可以通過action(forKey:來檢查動作是否正在運行。

我已經做了更詳細的例子來告訴你它怎麼可能獲得多點觸控在雪碧包你的行動:

import SpriteKit 
class GameScene: SKScene { 
    private var player1 : SKSpriteNode! 
    private var player2 : SKSpriteNode! 
    override func didMove(to view: SKView) { 
     self.player1 = SKSpriteNode.init(color: .red, size: CGSize(width:50,height:50)) 
     self.player2 = SKSpriteNode.init(color: .yellow, size: CGSize(width:50,height:50)) 
     addChild(player1) 
     addChild(player2) 
     self.player1.position=CGPoint(x:self.frame.width/2,y:(self.frame.height/2)+100) 
     self.player2.position=CGPoint(x:self.frame.width/2,y:(self.frame.height/2)-100) 
     if let view = self.view { 
      view.isMultipleTouchEnabled = true 
     } 
    } 
    func rightActions() { 
     if (player1.action(forKey: "right") == nil) { 
      player1.run(SKAction.moveTo(x: 0 + player1.size.width/2, duration: 0.1),withKey:"right") 
     } 
     if (player2.action(forKey: "right") == nil) { 
      player2.run(SKAction.moveTo(x: 0 + player1.size.width + player2.size.width/2, duration: 0.1),withKey:"right") 
     } 
    } 
    func leftActions() { 
     if (player1.action(forKey: "left") == nil) { 
      player1.run(SKAction.moveTo(x: self.frame.width - player1.size.width - player2.size.width/2, duration:0.1) ,withKey:"left") 
     } 
     if (player2.action(forKey: "left") == nil) { 
      player2.run(SKAction.moveTo(x: self.frame.width - player1.size.width/2, duration: 0.1),withKey:"left") 
     } 
    } 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     for t in touches { 
      let touch = t as UITouch 
      let location = touch.location(in: self) 
      let touchedNode = self.atPoint(location) 
      let tapCount = touch.tapCount 
      print("tapCount: \(tapCount) - touchedNode:\(touchedNode) - location:\(location)") 
      location.x < (self.size.width/2) ? rightActions() : leftActions() 
     } 
    } 
} 
+0

我希望兩個節點時,我按住左分離和(SKAction.moveTo(x:0 + player1.size.width/2,duration:0.1),withKey:「right」)和另一個player2.run(SKAction .moveTo(x:self.frame.width - player1.size.width/2,duration:0.1),withKey:「left」).....對不起,我是新來的編碼,我可以得到它的工作: ( – BiserMarkov