仅在触摸时拖动 SKSpriteNode
Dragging an SKSpriteNode only when touched
我有一个 SKSpriteNode
,我想通过 touchesMoved
在屏幕上拖动它。但是,如果触摸不是首先发生在精灵的位置(不在场景中跳跃),我不希望精灵移动。
我有一个间歇性的解决方案,但我相信有些东西更优雅、更实用,因为这个解决方案不能正常工作——如果用户拖动精灵非常快。
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
var touchPoint : CGPoint = location
var muncherRect = muncherMan.frame
if (CGRectContainsPoint(muncherRect, touchPoint)) {
muncherMan.position = location
} else {
}
}
}
您可以实现这三个方法来实现您想要的:touchesBegan
、touchesMoved
和 touchesEnded
。当您触摸 muncherMan
时,将 muncherManTouched
设置为 true,这表明 muncherMan
可能会在下一帧中移动。完成移动或触摸时,设置 muncherManTouched
false 并等待下一次触摸。
这是示例代码。我为 muncherMan
添加了一个名称,这样我们就可以确定 touchesBegan
.
中哪个节点被触摸了
var muncherManTouched = false
override func didMoveToView(view: SKView) {
/* Setup your scene here */
...
muncherMan.name = "muncherMan"
self.addChild(muncherMan)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let nodeTouched = self.nodeAtPoint(location)
if (nodeTouched.name == "muncherMan") {
muncherManTouched = true
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if (muncherManTouched == true) {
muncherMan.position = location
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
muncherManTouched = false
}
我有一个 SKSpriteNode
,我想通过 touchesMoved
在屏幕上拖动它。但是,如果触摸不是首先发生在精灵的位置(不在场景中跳跃),我不希望精灵移动。
我有一个间歇性的解决方案,但我相信有些东西更优雅、更实用,因为这个解决方案不能正常工作——如果用户拖动精灵非常快。
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
var touchPoint : CGPoint = location
var muncherRect = muncherMan.frame
if (CGRectContainsPoint(muncherRect, touchPoint)) {
muncherMan.position = location
} else {
}
}
}
您可以实现这三个方法来实现您想要的:touchesBegan
、touchesMoved
和 touchesEnded
。当您触摸 muncherMan
时,将 muncherManTouched
设置为 true,这表明 muncherMan
可能会在下一帧中移动。完成移动或触摸时,设置 muncherManTouched
false 并等待下一次触摸。
这是示例代码。我为 muncherMan
添加了一个名称,这样我们就可以确定 touchesBegan
.
var muncherManTouched = false
override func didMoveToView(view: SKView) {
/* Setup your scene here */
...
muncherMan.name = "muncherMan"
self.addChild(muncherMan)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let nodeTouched = self.nodeAtPoint(location)
if (nodeTouched.name == "muncherMan") {
muncherManTouched = true
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if (muncherManTouched == true) {
muncherMan.position = location
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
muncherManTouched = false
}