patternMinor
Creating a class which has a method running every frame
Viewed 0 times
methodcreatingeveryhasrunningwhichframeclass
Problem
I’m working in
Recently, I asked the question which asked if there was a way to create a method in a new class which automatically ran itself every frame. I was answered that it would be a good idea to use
I also forgot to mention that I was using
As a result, I managed to invent my own solution which was to created a new category:
The extension works by adding 2 new methods to all
The
The idea behind this was that the topmost
Objective-C, SKSpriteKit and am trying to create a class which will work in both OS X and iOS for flexibility.Recently, I asked the question which asked if there was a way to create a method in a new class which automatically ran itself every frame. I was answered that it would be a good idea to use
CADisplayLink. However, I soon realised I could not, as it is exclusive to iOS — and I had forgotten to specify that I wanted this class to be usable in both platforms. This was quite a surprise, and I had to dig deep into the system’s .h files to be sure this was true.I also forgot to mention that I was using
SKSpriteKit and that my class was a subclass of SKSpriteNode in the question. So I then tried to add the method which needed to be ran every frame using SKActions and performSelector:. However, the end result was undesirable, as I observed 2 identical nodes of this class (called PhysicsObject) move across the screen at different speeds. Using SKActions and runBlock: was even worse, as the runBlock: action can’t be copied when the node is copied.As a result, I managed to invent my own solution which was to created a new category:
SKNode (updateExtension). The SKNode(updateExtension).h file (which is quite short) is pasted below.The extension works by adding 2 new methods to all
SKNodes, and hence all subclasses of SKNode:- (void)update:(NSTimeInterval)currentTime;
- (void)updater:(NSTimeInterval)currentTime;The
update: method works by calling the node’s own updater: method, and then calling all of its children’s update: methods. The updater: method of any particular node is then filled with custom code of the user’s choice (if left blank, it will do nothing). The update: method then cannot be touched.The idea behind this was that the topmost
node of the node tree would then be able to call all of its descendants’ updater: methods which would only take one line of code. Or evSolution
In this case, I would recommend using the built in
Why do you need the nodes to automatically update themselves? It seems like this could lead to a situation where you do not know what exactly is causing things to happen to the nodes in the scene. Furthermore, it seems like you are putting all of the logic for the game into the scene, breaking encapsulation between the game model and the way it is rendered.
Carefully reading over your question, it appears that you already know about this method. I recommend that you use that method and make the updates to the appropriate nodes inside that method (calling their update methods from there). If you confine all changes of game state to this method, then it will be easier to keep track of what happens each frame. The individual nodes could have a state that would determine whether anything happens during their update.
SpriteKit methods for the best readability and the best control of the state while the game is running.Why do you need the nodes to automatically update themselves? It seems like this could lead to a situation where you do not know what exactly is causing things to happen to the nodes in the scene. Furthermore, it seems like you are putting all of the logic for the game into the scene, breaking encapsulation between the game model and the way it is rendered.
SKScenes have a built in method that is called every frame: - (void)update:(NSTimeInterval)currentTime. Carefully reading over your question, it appears that you already know about this method. I recommend that you use that method and make the updates to the appropriate nodes inside that method (calling their update methods from there). If you confine all changes of game state to this method, then it will be easier to keep track of what happens each frame. The individual nodes could have a state that would determine whether anything happens during their update.
Context
StackExchange Code Review Q#108768, answer score: 2
Revisions (0)
No revisions yet.