问题
I have two questions about having multiple level scenes.
There are multiple scenes for different levels. All those scenes make use of the same Bit Mask Categories and other variables defined in their .h file. Is there a way to define the Bit Mask Categories and other variables in one single file, in stead of the .h file of every single level scene?
In the update method of a level scene, I detect if the float 'score' is higher or equal to 100. If that is the case, change the scene to the next level. But because the update method runs every frame, It just freezes and tries to change the scene over and over. Is there a way to run an if-statement in the update method just once?
回答1:
1 - Subclassing is the answer. Create a class BaseScene, which is a subclass of SKScene. Include all the common elements of all the scenes here. This includes not only the bitmask categories, but also any methods or other properties that the scenes 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
while initialising, and then in the code where you check your score within the -update
method, do something like this:
if (!scoreReached)
{
if (score >= 100)
{
//Do whatever is needed
scoreReached = YES;
}
}
来源:https://stackoverflow.com/questions/21777548/sprite-kit-defining-the-variables-for-multiple-scenes