HTML and CSS Reference
In-Depth Information
Q.stages[num].destroy();
Q.stages[num] = null;
}
};
Q.clearStages = function() {
for(var i=0,len=Q.stages.length;i<len;i++) {
if(Q.stages[i]) { Q.stages[i].destroy(); }
}
Q.stages.length = 0;
};
Much like Q.scene , the Q.stage helper method returns a specific stage. It has a little bit of added com-
plication in that the engine keeps track of an activeStage , which represents the stage currently stepped and
drawn so that it can be referenced more easily by sprites and other parts of the engine.
Unlike Q.scene , which worked as both a getter and a setter, to add a new stage to the game, the engine
provides a different method, called Q.stageScene , to take a scene and present it on a stage. This method can
be called with anywhere from 0 to 3 parameters. In the first case, when no parameters are passed, the method
creates a new, empty stage on at the first slot. When passed all three parameters, the developer has control over
the scene to be staged: the stage slot used and the stage class. Because Q.Stage is a normal, extensible class,
it stands to reason the module that would want to extend the default stage functionality (such as adding in more
advanced collision detection algorithms) can do so and call Q.stageScene with that stage class.
The actual code of Q.stageScene is straightforward. It looks up a scene object if a string is passed
in, destroys the existing stage at that slot if there is one, and then creates a new stage object at the proper
slot. Finally, it checks to see if the game loop has already been started; if not, it runs the loop with a special
Q.stageGameLoop method (defined next) that ensures all the active steps are updated and rendered for each
scene. This means the first time you call Q.stageScene the engine takes care of starting the appropriate
game loop automatically.
The Q.stageGameLoop method, which is passed to Q.gameLoop as just described, clears the context,
if there is one, and then loops over each of the stages in the Q.stages array (which may not have values at
each index, so care is needed to ensure there is a valid stage object at any index). It then calls the step and
draw methods of each stage, setting the Q.activeStage property so that any sprites on that stage can call
Q.stage() to get the currently active stage object because they will need to for collision detection, for ex-
ample.
NOTE You might be wondering why the engine spends extra effort on supporting multiple stages active at a
time when one stage can happily support as many objects as necessary. Although there are other reasons, the
primary reason is to make it easy to add layered game interface screens on top of the current game. If you
were developing an RPG, for example, you would want to easily pop up an inventory screen on top of the
game screen and pause the game. By adding a little bit of engine support for both of these features, the en-
gine makes life much easier down the road for this sort of functionality without adding too much complexity
to the engine.
 
Search WWH ::




Custom Search