Game scene consists of background, menu button, score, snake and drops (snake's food). In init function we create all needed objects and create initial drops distribution.
Example: init function to create all needed objects
function init()
{
super.init(); this._initVariables(); //init helper variables
this._loadBg(); //load background image
this._createMenuBttn(); //create and add to the scene menu button
this._createScore(); //create and add to the scene score
this._loadStage(); //load stage from xml
this._createBigDrops(); //create and distribute big drops
this._createDrops(); //create and distribute drops
this._createSnake(); //create snake object
}
Function _createDrops distributes drops to the playground. The drops are distributed into random positions. However, we have to ensure that two foods will not overlap.
Firstly, we create new Drop object. Then - in while cycle - we try to find a location on the screen where our object isn’t in collision with any others. We get random position by function rand(Integer). We must detect, if there isn’t a collision with other objects. If everything is ok, we can jump from while cycle by using Stop variable. At the end, we add the drop to the our array this.food and to the scene.
Example: distribute drops to random positions
function _createDrops()
{
for (var i = 0; i < this.stage.drop_count; i++) {
var b = new Drop();
//Random position
var stop = false;
while (!stop) {
stop = true;
b.x = (1+rand(this.width/b.width-2))*b.width-10;
b.y = (1+rand(this.height/b.height-2))*b.height-10; for (var i in this.food)
if (b.intersectsBounds(i)) // check if new position do not overlap with previous position
stop = false;
} //Add object to the container
this.food.push(b);
this.add(b);
}
}
Another important step is to to create Snake object. It is very simple, because all the work is done in the Snake class. All we need to do is to create instance of Snake class with necessary parameters. You can learn more about Snake class in section Sprites/Snake.
We didn’t add snake object to the scene using function this.add(Object). It is because snake isn’t extended from Sprite class, but it is our own class which manages and synchronizes numerous sprites objects (head, body, tail). Therefore, we must explicitly call snake methods, which will operate the events from the scene.
Example: create snake
function _createSnake()
{
this.snake = new Snake(this.stage.start_x, this.stage.start_y, this.stage.start_body_count, this.stage.start_direction);
this.snake.timer.sleep = this.stage.start_refresh;
}
This event draws background,snake and it also calls the draw function for each sprite object in scene.
/**
Function call at redrawing.
@param canvas Canvas object.
*/
function draw(canvas)
{
//draw background
canvas.drawBitmap(this._background, 0, 0);
//draw snake
this.snake.draw(canvas);
//call parents draw function
super.draw(canvas);
}
Similarly in event process we must explicitly call this.snake.onProcess function.
/**
* Function called each 25 millisecond.
* @return nothing
*/
function process()
{
super.process(); if (this._pause)
return; if (this.snake.onProcess(this))
this._pushed = false;
}
|