Swift 两个节点的快速多点触摸

Swift 两个节点的快速多点触摸,swift,sprite-kit,Swift,Sprite Kit,这就是当你点击屏幕的左侧和右侧时,我如何使我的节点移动,但是当一个手指在左侧和另一个手指在右侧时,我如何使它们分开 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let location = (touches.first)?.location(in: self.view) if (location?.x)! < (self.view

这就是当你点击屏幕的左侧和右侧时,我如何使我的节点移动,但是当一个手指在左侧和另一个手指在右侧时,我如何使它们分开

    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))
        }
    }
override func touchsbegind(touch:Set,带有事件:UIEvent?){
让位置=(touch.first)?.location(在:self.view中)
if(位置?.x)!<(self.view?.bounds.size.width)!/2{
player1.run(SKAction.moveTo(x:0+player1.size.width/2,持续时间:0.1))
player2.run(SKAction.moveTo(x:0+player1.size.width+player2.size.width/2,持续时间:0.1))
}否则{
player1.run(SKAction.moveTo(x:self.frame.width-player1.size.width-player2.size.width/2,持续时间:0.1))
player2.run(SKAction.moveTo(x:self.frame.width-player1.size.width/2,持续时间:0.1))
}
}

要在游戏中启用多点触控,应使用:
view.isMultipleTouchEnabled=true
。您可以找到API引用

然后,如果您从SKS文件呈现场景,通常设置的
主播点是
(0.5,0.5)
,因此要使用下面的示例,请确保在
GameViewController
中添加这一行:

scene.anchorPoint = CGPoint.zero
在行
view.presentScene(scene)
之前,将当前场景的
主播点设置为0

您还应该考虑“保护”您的操作不受多个调用的影响,特别是当您从触摸事件委托方法启动这些操作时。这可以通过
操作(forKey:
检查操作是否正在运行)实现

我制作了一个更详细的示例,向您展示如何在Sprite kit中为您的动作获得多点触控:

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()
        }
    }
}
导入SpriteKit
类游戏场景:SKScene{
二等兵var player1:SKSpriteNode!
私人var播放器2:SKSpriteNode!
覆盖func didMove(到视图:SKView){
self.player1=SKSpriteNode.init(颜色:红色,大小:CGSize(宽度:50,高度:50))
self.player2=SKSpriteNode.init(颜色:黄色,大小:CGSize(宽度:50,高度: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)
如果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,持续时间:0.1),按键:“右”)
}
if(player2.动作(forKey:“right”)==nil){
player2.run(SKAction.moveTo(x:0+player1.size.width+player2.size.width/2,持续时间:0.1),按键:“右”)
}
}
func leftActions(){
if(player1.动作(forKey:“左”)==nil){
player1.run(SKAction.moveTo(x:self.frame.width-player1.size.width-player2.size.width/2,持续时间:0.1),键为“左”)
}
if(player2.动作(forKey:“左”)==nil){
player2.run(SKAction.moveTo(x:self.frame.width-player1.size.width/2,持续时间:0.1),使用键“left”)
}
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
对于t-in-touch{
让触摸=t作为UITouch
让位置=触摸。位置(in:self)
让touchedNode=self.atPoint(位置)
让tapCount=touch.tapCount
打印(“tapCount:\(tapCount)-touchedNode:\(touchedNode)-location:\(location)”)
location.x<(self.size.width/2)?rightActions():leftActions()
}
}
}

我希望当我按住屏幕的左侧和右侧时,两个节点分开,第一个操作是player1.run(SKAction.moveTo(x:0+player1.size.width/2,持续时间:0.1),键为:“right”),另一个操作是player2.run(SKAction.moveTo(x:self.frame.width-player1.size.width/2,持续时间:0.1),键为“left”)…很抱歉,我对编码不熟悉,我可以让它工作:(