gpt4 book ai didi

ios - 在 SpriteKit 中允许触摸传播

转载 作者:行者123 更新时间:2023-12-01 21:58:08 25 4
gpt4 key购买 nike

给定一个 SpriteKit现场s , 和一个节点 a类型 T继承自 SKSpriteNode包含在 s 中, 如果两者都是 sa覆盖任何触摸处理程序,触摸事件仅在似乎是最顶层(最高 zPosition)的节点上调用。

如果我希望场景及其节点同时执行两个不同的操作,最好使用哪种模式?

在这种情况下,最好是:

  • 让场景处理全部 通过检索触摸发生位置的节点来进行触摸?
  • 有一个Touchable场景将实现的协议(protocol),并让任何节点通过假设的 touched(_ nodeID: Int) 向场景发送标识符方法?
  • 让所有子节点引用静态 TouchHandler将执行这两个操作的对象?

  • 你还有什么建议?

    更新 :

    给定以下代码:
    class Parent: SKScene {
    override func didMove(to view: SKView) {
    self.addChild(Foo())
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // React to touch, and, for example, move the scene around
    }
    }

    class Foo: SKSpriteNode {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // React to touch, and, for example, scale up the node by 60%
    }
    }

    如果用户点击 Foo 节点,屏幕上只显示它的 touchesBegan(_, event:)方法被调用 - Parent的方法被忽略。

    为了使两个对象都能对 touchesBegan(_, event:) 使用react,最好使用哪种模式?同时回调 ?

    最佳答案

    我喜欢尽可能多地处理对象类中的代码。因此,我将处理其类文件中的任何对象触摸代码,并将触摸发送回场景以由场景单独处理。

    class Cat: SKSpriteNode {

    var isEnabled = true
    var sendTouchesToScene = true

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

    guard isEnabled else { return }

    //send touch to scene
    if sendTouchesToScene {
    super.touchesBegan(touches, with event)
    }

    //handle touches for cat
    }
    }

    关于ios - 在 SpriteKit 中允许触摸传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61010180/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com