【发布时间】:2015-05-16 22:03:38
【问题描述】:
我在屏幕上有移动精灵。现在我确实抓住了触摸,如果精灵被触摸 - 我将其移除。但我想把它刷掉。含义 - 我扫过它(任何方向),如果它是正确的精灵(检查名称) - 删除它。
我确实有一个触摸码,但我认为它不需要在这里粘贴,它非常标准,刷卡码肯定会有所不同。
有什么建议吗?谢谢!
【问题讨论】:
标签: ios swift sprite-kit sprite swipe
我在屏幕上有移动精灵。现在我确实抓住了触摸,如果精灵被触摸 - 我将其移除。但我想把它刷掉。含义 - 我扫过它(任何方向),如果它是正确的精灵(检查名称) - 删除它。
我确实有一个触摸码,但我认为它不需要在这里粘贴,它非常标准,刷卡码肯定会有所不同。
有什么建议吗?谢谢!
【问题讨论】:
标签: ios swift sprite-kit sprite swipe
试试这个:
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)
{
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location)
{
//remove sprite here
}
}
另外,如果您希望它在触摸时被移除,也可以添加:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location)
{
//remove sprite here
}
}
【讨论】:
您可以使用UISwipeGestureRecognizer 并获取开始触摸位置。之后,您可以检查哪个节点位于该位置。
这样:
override func didMoveToView(view: SKView) {
//create Swipe gesturerecognizer
var swipe = UISwipeGestureRecognizer()
//set Direction to Left
swipe.direction = .Left
//Call method "swipe" if swipe is done
swipe.addTarget(self, action: "swipe:")
self.view!.addGestureRecognizer(swipe)
}
func swipe(sender: UISwipeGestureRecognizer){
//get amount of touches in swipe
var touches = sender.numberOfTouches()
//loop through touches
for touch in 0..<touches{
var location = sender.locationOfTouch(touch, inView: self.view)
//Get the node at the location of the touch
var swipedNode = self.nodeAtPoint(location)
}
}
【讨论】:
numberOfTouches 获取触摸次数,然后对其进行迭代并检查当前触摸的节点。