Sprite Kit - Defining the variables for multiple scenes

佐手、 提交于 2019-12-25 02:26:10

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!