swift skscene触摸并保持其他动作,而不仅仅是触摸

时间:2017-07-02 20:09:30

标签: ios swift sprite-kit nodes skscene

所以我最近用sprite套件做了一个快速的游戏,现在我被卡住了。 我有一个角色选择屏幕,如果你按住角色,我想显示角色的描述,但如果你只是触摸它,你选择它并玩它。我已经有代码来播放/显示描述。我只需要知道何时调用相应的函数以及如何区分节点是保持还是刚触及。

提前致谢

2 个答案:

答案 0 :(得分:0)

所以,这是你如何使用SKActions来做到这一点...另外,还有另一种使用SKAction的方式,但我无法真正向你展示所有的可能性:)无论如何,这里是做你想做的代码:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    let kLongPressDelayActionKey = "longPressDelay"
    let kLongPressStartedActionKey = "longPressStarted"
    let kStoppingLongPressActionKey = "stoppingLongPressActionKey"

    let desc = SKSpriteNode(color: .white, size: CGSize(width: 150, height: 150))
    let button = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 150))
    let other = SKSpriteNode(color: .yellow, size: CGSize(width: 150, height: 150))
    override func didMove(to view: SKView) {

        addChild(desc)
        addChild(button)
        addChild(other)

        button.position.y = -160
        button.name = "button"
        desc.alpha = 0.0
        desc.name = "description"
        other.name = "other"
        other.alpha = 0.0
        other.position.y = -320

    }

    private func singleTap(onNode:SKNode){

        other.alpha = other.alpha == 0.0 ? 1.0 : 0.0
    }

    private func startLongPress(withDuration duration:TimeInterval){

        //How long does it take before long press is fired
        //If user moves his finger of the screen within this delay, the single tap will be fired
        let delay = SKAction.wait(forDuration: duration)

        let completion = SKAction.run({
        [unowned self] in

             self.desc.removeAction(forKey: self.kLongPressDelayActionKey)
             self.desc.run(SKAction.fadeIn(withDuration: 0.5), withKey: self.kLongPressStartedActionKey)
        })

        self.desc.run(SKAction.sequence([delay,completion]), withKey: kLongPressDelayActionKey)

    }

    private func stopLongPress(){

        //Fire single tap and stop long press
        if desc.action(forKey: kLongPressDelayActionKey) != nil{

            desc.removeAction(forKey: kLongPressDelayActionKey)
            self.singleTap(onNode: self.other)
        //or just stop the long press
        }else{

            desc.removeAction(forKey: kLongPressStartedActionKey)

            //Start fade out action if it isn't already started
            if desc.action(forKey: kStoppingLongPressActionKey) == nil {
                desc.run(SKAction.fadeOut(withDuration: 0.2), withKey: kStoppingLongPressActionKey)
            }
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        //To stop the long press if we slide a finger of the button
        if let touch = touches.first {

            let location = touch.location(in: self)

            if !button.contains(location) {

              stopLongPress()

            }

        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first {

            let location = touch.location(in: self)

            if button.contains(location) {
                print("Button tapped")

                startLongPress( withDuration: 1)
            }
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

       stopLongPress()
    }
}

您可以运行代码,您会看到如果点击紫色按钮,会弹出一个黄色框。如果再次点击它,它将隐藏。此外,如果您将手指放在紫色框上1秒钟,则白色框会淡入。当您松开手指时,它会淡出。另外,我已经实现了touchesMoved,所以如果你在蓝色框出来时滑动紫色按钮的手指,它也会淡出。

所以在这个例子中,我将长按与单击融合。单击被认为是不长按的一切。例如,如果用户握住他的手指0.01秒,或0.5秒,或0.99秒,则将其视为单击并且将弹出黄色框。如果你的手指按> = 1秒,将触发长按动作。

另一种方法是使用手势识别器...我稍后可能会为此做一个例子:)

答案 1 :(得分:0)

以下是使用手势识别器的方法:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    var longPressGesture:UILongPressGestureRecognizer!
    var singleTapGesture:UITapGestureRecognizer!

    let kLongPressStartedActionKey = "longPressStarted"
     let kStoppingLongPressActionKey = "stoppingLongPressActionKey"

    let desc = SKSpriteNode(color: .white, size: CGSize(width: 150, height: 150))
    let button = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 150))
    let other = SKSpriteNode(color: .yellow, size: CGSize(width: 150, height: 150))

    override func didMove(to view: SKView) {

        longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(GameScene.longPress(_:)))
        singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(GameScene.singleTap(_:)))
        self.view?.addGestureRecognizer(longPressGesture)
        self.view?.addGestureRecognizer(singleTapGesture)
        addChild(desc)
        addChild(button)
        addChild(other)

        button.position.y = -160
        button.name = "button"
        desc.alpha = 0.0
        desc.name = "description"
        other.name = "other"
        other.alpha = 0.0
        other.position.y = -320

    }

    private func stopLongPress(){



        desc.removeAction(forKey: self.kLongPressStartedActionKey)

        //Start fade out action if it isn't already started
        if desc.action(forKey: kStoppingLongPressActionKey) == nil {
            desc.run(SKAction.fadeOut(withDuration: 0.2), withKey: kStoppingLongPressActionKey)
        }

    }

    func singleTap(_ sender:UITapGestureRecognizer){


        if sender.state == .ended {

            let location = convertPoint(fromView: sender.location(in: self.view))

            if self.button.contains(location) {
                    self.other.alpha = self.other.alpha == 0.0 ? 1.0 : 0.0
            }
        }
    }

    func longPress(_ sender: UILongPressGestureRecognizer) {
        let longPressLocation = convertPoint(fromView: sender.location(in: self.view)) 

        if sender.state == .began {

            if button.contains(longPressLocation) {
                desc.run(SKAction.fadeIn(withDuration: 0.5), withKey: self.kLongPressStartedActionKey)
            }

        }else if sender.state == .ended {
            self.stopLongPress()
        }else if sender.state == .changed {


            let location = convertPoint(fromView:  sender.location(in: self.view))


            if !button.contains(location) {

                stopLongPress()

            }

        }
    }
}