0

I have two questions about having multiple level scenes.

  1. 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?

  2. 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?

user2255273
  • 948
  • 1
  • 11
  • 29
  • For number 1) create a header file (in Xcode NewFile-> c and c++ -> Header file) define all your bit masks, and other shared variables, include it where ever you need bit masks. – Dobroćudni Tapir Feb 14 '14 at 11:09

1 Answers1

3

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;
    }
}
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
  • One small question though, where do I import BaseScene.m in the level scenes? So I can use the methods that are in BaseScene.m. – user2255273 Feb 14 '14 at 11:42
  • If you have imported BaseScene.h, then BaseScene.m will be automatically included. To be able to access the methods in BaseScene.m, you should declare them in BaseScene.h. – ZeMoon Feb 14 '14 at 11:44