【问题标题】:In Swift and sprite kit how to detect swiped sprites?在 Swift 和 spritekit 中如何检测滑动精灵?
【发布时间】:2015-05-16 22:03:38
【问题描述】:

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

我确实有一个触摸码,但我认为它不需要在这里粘贴,它非常标准,刷卡码肯定会有所不同。

有什么建议吗?谢谢!

【问题讨论】:

    标签: ios swift sprite-kit sprite swipe


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 这很完美!我已经编辑了您的回复,将我的案例添加到 touchesMoved 函数中。可以这么说,我正在“滑动”期间检查精灵的名称。
    【解决方案2】:

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

    【讨论】:

    • 好的!但是是否可以在滑动期间检查“每个点”的节点?说开始是在没有节点的位置,但最终在滑动期间我与一个节点相交......?
    • @YuraAntonov 我已经更新了我的答案。您可以使用numberOfTouches 获取触摸次数,然后对其进行迭代并检查当前触摸的节点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多