SpriteKit中允许触摸传播

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

给出SpriteKit场景s,并从a中包含的T继承一个类型为SKSpriteNode的节点s,如果sa都覆盖任何触摸处理程序,则仅在最高(最高zPosition)节点上调用touch事件。

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

在这种情况下,最好是:

  • 让场景处理all通过获取发生触摸的位置处的节点来获取触摸?
  • 具有场景将要实现的Touchable协议,并且让任何节点通过假设的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:)回调做出反应,最好使用哪种模式?]

给出一个SpriteKit场景s,并从s中包含的SKSpriteNode继承的一个类型T的节点a,如果s和重写任何触摸处理程序,则仅在看起来...的情况下调用touch事件。]

ios sprite-kit skspritenode game-development skscene
1个回答
0
投票
我喜欢处理对象类中尽可能多的代码。因此,我将处理其类文件中的任何对象触摸代码,并将触摸发送回场景以由场景分别处理。

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 } }

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