簡體   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