[英]In Swift and sprite kit how to detect swiped sprites?
I have moving sprites on a screen. 我在屏幕上移动精灵。 Now I do catch the touch and if the sprite is touched - i remove it.
现在,我确实抓住了触摸,如果触摸了精灵,则将其删除。 But I want to swipe it away.
但是我想把它擦掉。 Meaning - i swipe through it (any direction) and if it's a correct sprite (check the name) - remove it.
含义-我在其中滑动(任何方向),并且如果它是正确的精灵(检查名称),则将其删除。
I do have a touch code, but i don't think it needs to be pasted here, it's pretty standard and swipe code will be different for sure. 我确实有一个触摸代码,但是我不认为需要在此处粘贴它,这是非常标准的,并且滑动代码肯定会有所不同。
any suggestions? 有什么建议么? thank you!
谢谢!
Try this: 尝试这个:
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
}
}
Also, if you want it to be removed on a touch, add this too: 另外,如果您希望将其轻轻一去,也可以添加以下内容:
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
}
}
You can use a UISwipeGestureRecognizer
and get the start-touch location. 您可以使用
UISwipeGestureRecognizer
并获取开始触摸的位置。 After that you can check which node is at that location. 之后,您可以检查哪个节点在该位置。
Like that: 像那样:
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.