In the Apple documentation here Advanced Scene Processing it describes the update
method and how a scene is rendered, but it does not mention when input is processed. It is not clear if this is in in the same thread as the rendering loop, or whether it is concurrent with it.
If I have an object that I update from both the SKScene
update
method and the touchesBegan
method (in this case of a SKSpriteNode
) do I have to worry about synchronising the two accesses to my object?
So after a few days with no answer I set up some experiments. By the way, these tests are run on the simulator and not on an actual device, but I think it would be the same.
First test, I set a breakpoint in the debugger on touchesBegan
and looked at the stack trace. It appears that touchesBegan
is called from the first thread and from the main
loop - the same place as the rest of the logic, so this looks good for a singe-threaded approach.
Second test, I overrode the various methods in the scene mentioned in the Advanced Scene Processing link above and added print statements to show th name of each function called. Then I added a print statement to the touchesBegan
method.
On running the app, the output was:
update
didEvaluateActions
didSimulatePhysics
didApplyConstraints
didFinishUpdate
touchesBegan in scene
update
didEvaluateActions
didSimulatePhysics
didApplyConstraints
didFinishUpdate
update
and this pattern was repeated whenever I clicked.
No amount of clicking gave me anything else than touchesBegan
being called between the didFinishUpdate
(that is, the end of one cycle) and the update
(the beginning of the next).
Conclusion: touches processing happens in the main loop before the update method is called. It is therefore not necessary to synchronise resources between the two methods.
来源:https://stackoverflow.com/questions/29755372/in-spritekit-does-touchesbegan-run-in-the-same-thread-as-the-skscene-update-meth