【问题标题】:How to run a function when a long press has ended swift sprite kit长按结束时如何运行功能快速精灵套件
【发布时间】:2020-04-17 20:08:52
【问题描述】:

我正在运行一个非常简单的代码,我正在尝试检测长按何时结束,然后执行一个函数,但我似乎不知道该怎么做。我在网上查看了很多资源,但我一直无法弄清楚。我对 Swift 编码很陌生,所以如果答案很简单,我提前道歉。这是所有代码:

import SpriteKit

class GameScene: SKScene {

    let char = SKSpriteNode(imageNamed: "flying character")

    override func didMove(to view: SKView) {
        setScene()
    }

    @objc func press() {
        physicsWorld.gravity = CGVector(dx: 0.1, dy: 1.5)
        if UILongPressGestureRecognizer.State.changed == .ended {
            print ("the gesture has ended")
        }
    }

    func setScene() {
        backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 221/255, alpha: 1.0)
        char.size = CGSize(width: frame.size.width/5, height: frame.size.width/5)
        char.run(SKAction.rotate(byAngle: .pi*11/6, duration: 0.00001))
        char.position = CGPoint(x: frame.midX - 100, y: frame.midY + 150)
        addChild(char)
        char.physicsBody = SKPhysicsBody(circleOfRadius: char.size.width/2)
        char.physicsBody?.categoryBitMask = PhysicsCategories.charCategory
        physicsWorld.gravity = CGVector(dx: 0.1, dy: -1.5)
        char.physicsBody?.affectedByGravity = true
        view?.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(press)))
    }
}

如您所见,我只是在长按结束后尝试执行打印命令。

【问题讨论】:

  • 使用touchesEnded方法?
  • 我没有遵循你的代码......你的重力变化将发生在所有事件上。您的意思是将重力变化置于结束事件中吗?

标签: ios swift user-interface sprite-kit 2d-games


【解决方案1】:

首先,您可能注意到的一件非常重要的事情是您的问题与 SpriteKit 本身无关!您正在使用 UILongPressGestureRecognizer,这是一个来自 UIKit 的类! Apple Docs

另外,请注意您将手势应用于整个视图。当我假设这是一个角色会飞的游戏时,我想这种行为是预期的。

我想您面临的问题是您没有使用分配给您的视图的 UILongPressGestureRecognizer!相反,您正在访问 UILongPressGestureRecognizer 类属性/方法。更多详情可以浏览this section of the Swift documentation


快速解决您的问题的方法是从

更改您的新闻功能
@objc func press() {
    physicsWorld.gravity = CGVector(dx: 0.1, dy: 1.5)
    if UILongPressGestureRecognizer.State.changed == .ended {
        print ("the gesture has ended")
    }
}

@objc func press(_ gestureRecognizer: UILongPressGestureRecognizer) {
    physicsWorld.gravity = CGVector(dx: 0.1, dy: 1.5)
    if gestureRecognizer.state == .ended {
        print ("the gesture has ended")
    }
}

你的 setScene 函数来自

view?.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(press)))

view?.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(press:)))

More information here


SpriteKit 以一种非常原始的方式处理触摸事件,但它可能会让您对如何响应这些事件有更多的控制。正如 KnightOfDragon 所说,这很像重新发明轮子。

如果你想看看 SpriteKit 是如何做到的,see this example. 很短,但我希望它有所帮助:)

【讨论】:

  • 我同意除了“SpriteKity”之外的所有内容。该手势适用于 SpriteKit,并且是 SpriteKit 方式。您只需要在识别器功能中处理翻译。您的示例所做的只是通过告诉用户他们需要重新发明轮子来提供如何处理触摸事件,而不是手势。
  • 很好!完全同意。编辑答案以更好地反映它:)
  • 我实际上只是将示例注释一起删除,哈哈。我会在处理手势时提到使用 SKScene 的 convertPoint(fromView point:CGPoint)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多