通过在SpriteKit / Swift中向上滑动来跳跃角色

问题描述 投票:0回答:1

目前我想为iOS编程跳转并运行游戏。在这个游戏中,角色应该通过向上滑动来跳起来。我知道角色可以通过以下功能跳起来:触摸屏幕上的applyimpulse(我也知道如何编程)。但是哪个功能可以通过在屏幕上向上滑动来跳跃,或者是否有相同的功能?

ios swift sprite-kit swift-playground
1个回答
0
投票

欢迎来到stackoverflow!

在GameScene中添加以下变量

// MARK: Gesture Recognizers
var upSwipe = UISwipeGestureRecognizer()

将以下行添加到didMove函数中,如下所示:

override func didMove(to view: SKView) {
    upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
    upSwipe.direction = .up
    view?.addGestureRecognizer(upSwipe)
}

这是魔术发生的地方:

@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
    if sender.state == .ended {
        switch sender.direction {
        case .up:
        // Call your jump function here
        jump()
        default:
            break
        }
    }
}

希望这对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.