2017-04-09 17 views
-1

これは、画面の左右をタップするとノードを移動させる方法ですが、左側に1本、左側に1本ずつ、右側?2つのノードの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 = trueを使用してください。

:あなたは SKSファイルからシーンを提示した場合 Hereあなたは通常setted anchorPointはそう私の例で動作するようにすると、下記のお GameViewControllerにこの行を追加してください (0.5,0.5)で、APIリファレンス次に

を見つけることができます

scene.anchorPoint = CGPoint.zero 

view.presentScene(scene)の前に、現在のシーンでanchorPointを0に設定してください。

特に、タッチイベントデリゲートメソッドからこれらのアクションを起動するときに、複数の呼び出しからアクションを保護すると考える必要があります。これは、アクションが実行中であるかどうかを確認するaction(forKey:で可能です。

私はスプライト・キットで自分の行動のためのマルチタッチを得ることが可能にすることができますどのようにお見せするために、より詳細な例を作った:私は左を保持する際に分離するために2つのノードが必要

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

と最初の操作は、player1.run(SKAction.moveTo(x:0 + player1.size.width/2、duration:0.1)、withKey: "right")、もう1つはplayer2.run(SKAction (x:self.frame.width - player1.size.width/2、duration:0.1)、withKey: "left").....申し訳ありませんが、私はコーディングに新しいので、動作させることができます: ( – BiserMarkov

関連する問題