问题
I am contemplating a simple app that has four characters that you can drag around on the screen. While dragging they "wiggle" — that's the animation. And they snap to a position if they get close enough to it... like in a puzzle. Without the animation, this is all simple in UIKit.
My first thought is to render each character in its own SKView
inside a plain old UIView
. I could attach UIGestureRecognizers
to each SKView
to track tapping and dragging. But I think this implies individual GameScenes
for each character/SKView
. That seems go go against the grain of SpriteKit.
The alternative is a single GameScene
with the four sprites. But I would still need to track & drag them and I don't see how to do that within an all-SpriteKit app.
Is either approach better practice than the other?
回答1:
You should implement the one that has a single scene and separate sprites for each character. The sprites will be SKSpriteNode
instances which are SKNode
instances, which descend from UIResponder
and can, therefore, respond to touch events.
Look at the documentation for SKNode
. It is a wealth of information that applies to sprites as well as to any other kind of node. In particular it says:
All nodes are responder objects that can respond directly to user interaction with the node onscreen…
And then later has a section on "The Hit-Test Order is the Reverse of Drawing Order" etc.
来源:https://stackoverflow.com/questions/52634395/using-spritekit-inside-uikit