Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在屏幕上移动SKSpriteNode_Swift_Touch_Skspritenode_Touchesbegan_Touchesmoved - Fatal编程技术网

Swift 在屏幕上移动SKSpriteNode

Swift 在屏幕上移动SKSpriteNode,swift,touch,skspritenode,touchesbegan,touchesmoved,Swift,Touch,Skspritenode,Touchesbegan,Touchesmoved,我想用触摸屏在屏幕上移动一个SKSpriteNode。我遇到的问题是,如果我锁定一个精灵,然后我的手指将它拖到另一个精灵上,第一个精灵被释放,我的手指开始拖第二个精灵。但一旦我触摸到第一个精灵,这是我唯一想移动的。 必须有一个简单的方法来解决这个问题 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { /* Called when a touch begins */

我想用触摸屏在屏幕上移动一个SKSpriteNode。我遇到的问题是,如果我锁定一个精灵,然后我的手指将它拖到另一个精灵上,第一个精灵被释放,我的手指开始拖第二个精灵。但一旦我触摸到第一个精灵,这是我唯一想移动的。 必须有一个简单的方法来解决这个问题

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */
    let touch: UITouch = touches.first as UITouch!
    touchLocation = touch.locationInNode(self)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch: UITouch = touches.first as UITouch!
    let newTouchLocation = touch.locationInNode(self)
    let targetNode = self.nodeAtPoint(touchLocation)
    if targetNode.physicsBody!.categoryBitMask != PhysicsCategory.Ball {
    targetNode.runAction(SKAction.moveBy(CGVector(point: newTouchLocation - touchLocation), duration: 0.0))
    touchLocation = newTouchLocation
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    //I don't do anything with this yet
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
让触摸:UITouch=touch.first作为UITouch!
touchLocation=touch.locationInNode(自)
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让触摸:UITouch=touch.first作为UITouch!
让newTouchLocation=touch.locationInNode(自)
让targetNode=self.nodeAtPoint(touchLocation)
如果targetNode.physicsBody!.categoryBitMask!=PhysicsCategory.Ball{
targetNode.runAction(SKAction.moveBy(CGVector(点:newTouchLocation-touchLocation),持续时间:0.0))
触摸位置=新触摸位置
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
//我还没有做任何事情
}

之所以会发生这种情况,是因为每次手指移动时,TouchsMoved功能都会运行,如果手指移动到新节点上,则该节点将分配给targetNode。您可以通过更改此行来解决此问题:

let targetNode = self.nodeAtPoint(touchLocation)
为此:

let targetNode = self.nodeAtPoint(touch)

这样,第一次触摸时的节点将是跟随手指的节点,而不是最后一次触摸时的节点

多亏了CodeComposer,我找到了解决方案:

var targetNode: SKNode!

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */
        let touch: UITouch = touches.first as UITouch!
        touchLocation = touch.locationInNode(self)
        targetNode = self.nodeAtPoint(touchLocation)
        objectTouched(touch.locationInNode(self))
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch: UITouch = touches.first as UITouch!
        let newTouchLocation = touch.locationInNode(self)
//        let targetNode = self.nodeAtPoint(touchLocation)
        if targetNode.physicsBody!.categoryBitMask != PhysicsCategory.Ball {
        targetNode.runAction(SKAction.moveBy(CGVector(point: newTouchLocation - touchLocation), duration: 0.0))
        touchLocation = newTouchLocation
        }
    }
var targetNode:SKNode!
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
让触摸:UITouch=touch.first作为UITouch!
touchLocation=touch.locationInNode(自)
targetNode=self.nodeAtPoint(触摸位置)
objecttouch(touch.locationInNode(self))
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让触摸:UITouch=touch.first作为UITouch!
让newTouchLocation=touch.locationInNode(自)
//让targetNode=self.nodeAtPoint(touchLocation)
如果targetNode.physicsBody!.categoryBitMask!=PhysicsCategory.Ball{
targetNode.runAction(SKAction.moveBy(CGVector(点:newTouchLocation-touchLocation),持续时间:0.0))
触摸位置=新触摸位置
}
}
我只定义

目标节点

一次,然后继续使用它,而不是在TouchsMoved中重新定义它:

它不起作用-“无法将“UITouch”类型的值转换为预期的参数类型“CGPoint”