在Swift和sprite kit中如何检测刷过的sprite?

时间:2015-05-16 22:03:39

标签: ios swift sprite-kit sprite swipe

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

我确实有触控密码,但我认为不需要粘贴在这里,它非常标准,滑动代码肯定会有所不同。

有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分:4)

试试这个:

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

答案 1 :(得分:0)

您可以使用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)
    }
}