Firsty all source files are included. The source file of Game class is game.ms located at lib://game2d/game.ms. An instance of this class is crated as a global variable directly after include commands.
// include framework classes
include "lib://core/log.ms";
include "lib://core/animation/transition.ms";
include "lib://game2d/game.ms";
... // include project classes
include "app://scene/menuScene.ms";
include "app://scene/gameScene.ms";
... // create new game instance
var game = new Game();
When the game starts game class runs onStart event. It is usally used to load images, init variables or create a game scenes. Scene class creates an objects which is independent. It has same events like game class and is used to devide the menu part from the game part. F.e.: also game levels can be created in separate scenes. Another advantage is that one scene can be used in more projects and should work same with only a few changes.
Example: onStart event
// prepare game instance before it runs
game.onStart = function()
{
// create menu scene
this.menu = new MenuScene();
this.push(this.menu);
// game scene will be in game variable
this.game = null;
}
When user click onto some hardwaere key the onKeyPressed event is called. It's second parameter is keyCode. The keyCode says which button was pressed. Supported sybols are #back, #home, #menu.
Example: onKeyPressed event
// reaction to hardware keyboard press
game.onKeyPressed = function(sender, keyCode)
{
if (keyCode == #back || keyCode == #home)
app.quit();
}
When everything is set the game can starts. To start is used run method, which opens the game window and starts all process.
Example: run the game
// run the game
game.run(true);
|