繁体   English   中英

在Swift和Sprite Kit中,如何检测滑动的Sprite?

[英]In Swift and sprite kit how to detect swiped sprites?

我在屏幕上移动精灵。 现在,我确实抓住了触摸,如果触摸了精灵,则将其删除。 但是我想把它擦掉。 含义-我在其中滑动(任何方向),并且如果它是正确的精灵(检查名称),则将其删除。

我确实有一个触摸代码,但是我不认为需要在此处粘贴它,这是非常标准的,并且滑动代码肯定会有所不同。

有什么建议么? 谢谢!

尝试这个:

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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM