我正在尝试实现一个覆盖的touchesMoved功能,以使SKSpriteNodes能够被用户移动。但是,我必须非常缓慢地移动节点,这样它才能在拖动时跟随我的触摸。此外,背景中还有三个SKSpriteNodes (您将看到我显式地设置为.userInteractionEnabled = false),这些节点偶尔会响应这些触摸。任何帮助都将不胜感激。如果您需要代码的其他部分,请告诉我。
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
var positionInScene = CGPoint(x: 375.0, y: 400.0) //sets a default position
titleLabel.userInteractionEnabled = false
drawingBoard.userInteractionEnabled = false
sideBar.userInteractionEnabled = false
for touch in touches {
positionInScene = touch.locationInNode(self)
if self.nodeAtPoint(positionInScene) is SKSpriteNode {
if (self.nodeAtPoint(positionInScene)).name == movableNodeName { //movableNodeName is the name assigned to all SKSpriteNodes that should be draggable
//I know this might be a strange way of doing it
(self.nodeAtPoint(touch.previousLocationInNode(self))).position = positionInScene
}
}
}
}发布于 2016-02-15 18:41:01
我成功地解决了这个问题,只需设置节点的坐标,而当节点被移动时,它就不应该移动回原来的位置。这似乎是可行的,因为我还没有能够在测试中再次复制该bug。如果有人能想出更好的解决方案,我很想听听。
发布于 2016-02-15 18:44:32
您的问题是您正在使用touch.locationInNode(self),而self是您的SKSpriteNode。这意味着您的触摸移动代码将只响应您的SKSpriteNode内部正在发生的事情。您需要做的是,根据您想要应用这个逻辑的方式,使用sprite的父级或场景。
https://stackoverflow.com/questions/34959410
复制相似问题