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

Posted

技术标签:

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

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

【问题讨论】:

How to add a swipe gesture to a node in spritekit的可能重复 【参考方案1】:

欢迎使用 ***!

在您的 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
        
    

希望对你有所帮助。

【讨论】:

以上是关于通过在 SpriteKit/Swift 中向上滑动来跳跃角色的主要内容,如果未能解决你的问题,请参考以下文章