【问题标题】:Character jump up by swiping up in SpriteKit/Swift通过在 SpriteKit/Swift 中向上滑动来跳跃角色
【发布时间】:2019-03-28 20:07:58
【问题描述】:

目前我想为 iOS 编写一个跳跃和跑步游戏。在这个游戏中,角色应该通过向上滑动来跳跃。我知道角色可以通过以下功能跳起来:通过触摸屏幕来应用脉冲(我也知道如何编程)。但是在屏幕上向上滑动可以让角色跳起来有什么功能呢,还是有同样的功能呢?

【问题讨论】:

标签: ios swift sprite-kit swift-playground


【解决方案1】:

欢迎使用 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
        }
    }
}

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    相关资源
    最近更新 更多