In a SpriteKit scene, a number of methods are called automatically by the system and you can override them in your game scene to implement custom behavior. For example:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
override func didFinishUpdate() {
// Do some stuff immediately before rendering
}
Apple's documentation for SpriteKit has this helpful graphic which shows the order in which these methods are called during each frame:
However, this discussion does not include any of the touch events, such as touchesBegan(_:with:). I'm trying to figure out at what point these touch events are called in relation to the other events in SpriteKit's frame cycle. I can't find any Apple documentation that gives an answer.
The only mention I could find was at the doc linked above, which says:
The scene’s
update(_:)method is called with the time elapsed so far in the simulation. This is the primary place to implement your own in-game simulation, including input handling, artificial intelligence, game scripting, and other similar game logic. Often, you use this method to make changes to nodes or to run actions on nodes.
Although it says that the update(_:) method is the place to implement input handling, that seems a bit misleading, as it's common practice to use the touch event methods like touchesBegan(_:with:) to capture touch input on iOS. SKScene inherits these touch method as a subclass of UIResponder (SKScene > SKEffectNode > SKNode > UIResponder). But I'm trying to determine when those methods are called within the order of the scene's frame-cycle events. This can be important to know when you are trying to make certain touch-dependent things happen in a specific order within your scene's frame cycle.
My guess would be that the touch methods are called at the start of the frame cycle, just prior to when the update(_:) method is called. But I'm looking for a more definitive answer than just my hunch.

After some experimenting, I determined that touch events are called after
didFinishUpdate()but beforeupdate(_:).When I ran this code and then touched the screen, here is what the console printed:
You can see that the touch event is still printing the same frame count value from the last update. And the next print message that runs is from the
update(_:)method with an updated frame count value.Now, this does not tell us if the
touchesBegan(_:with:)method was called at the end of the SpriteKit frame cycle, or if it was called at the start of the frame cycle. We only know that it was called somewhere in between the calls todidFinishUpdate()andupdate(_:). But that answer should be precise enough for most purposes.I will go further and say it's pretty safe to assume that the touch methods are called at the beginning of the frame cycle. It would not make sense to call them at the end of the cycle, because at that point we can't use them to do anything useful, since we can't run any code at that point in the cycle. It makes the most sense for the touch events to be called at the beginning of the frame cycle so that we can use them in the
update(_:)method (or one of the other frame cycle methods).