给出SpriteKit
场景s
,并从a
中包含的T
继承一个类型为SKSpriteNode
的节点s
,如果s
和a
都覆盖任何触摸处理程序,则仅在最高(最高zPosition
)节点上调用touch事件。
如果我希望场景及其节点同时执行两个不同的动作,则最好使用哪种模式?
在这种情况下,最好是:
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事件。]
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
}
}