简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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