Sprite Kit - variable definition for multiple scenes

I have two questions about several level scenarios.

  • There are several scenes for different levels. All these scenes use the same categories of bit masks and other variables defined in their .h file. Is there a way to define categories of bit masks and other variables in one file instead of a .h file for each level scene?

  • In the level scene update method, I find that the float value is greater than or equal to 100. If so, change the scene to the next level. But since the update method launches every frame, it just freezes and tries to change the scene again and again. Is there a way to run the if-statement in the update method only once?

0
source share
1 answer

1 - A subclass is the answer. Create the BaseScene class, which is a subclass of SKScene. Include all the common elements of all the scenes here. This includes not only the categories of bitmasks, but also any methods or other properties that the scene may have. This will improve the length of your code besides solving your problems.

Make all your level scenes a subclass of BaseScene.

2 - Create a Bool variable called scoreReached or something in your code. Set this variable to NO during initialization, and then in the code where you check your result in the -update method, follow these steps:

 if (!scoreReached) { if (score >= 100) { //Do whatever is needed scoreReached = YES; } } 
+2
source

All Articles