Layers - basic object that usually covers whole screen and manages other tiled and sprite objects. It also provides operations, such as scrolling backgrounds using setViewPos(). Other objects can be added to the layer by append(). Layers class has some common properties like width, height, top, left and native to access native view object, included in the layers class. The class also has onAfterDraw() function, which is called every time after the class processed native onDraw() function. It means that Layers class firstly redraws all included objects and then calls this event. This is a good function if drawing additional texts is required.
// add application class
include "code://ui/application.ms"
// add skin class
include "code://skin/skin.metro.ms"
// add source game 2d classes
include "code://game2d/layers.ms"
include "code://game2d/tiled.ms"
include "code://game2d/sprite.ms" var app = new Application("sample game2d application", new MetroSkin()); app.onStart = function()
{
// create new layer object
this._layers = new Layers();
// set layer size and position
this._layers.width = System.width;
this._layers.height = System.height;
this._layers.left = 0;
this._layers.top = 0; // create paint objects for fps text drawing
this._paint = new Paint();
this._paint.color = 0xffffffff;
// set drawing function (this function is called when all layers objects were drawenrendered)
this._layers.onAfterDraw = function(layers, canvas)
{
// draw fps string
canvas.drawText(System.avgFPS.toString(), 10, System.height / 20, this super._paint);
}
app.add(this._layers);
} app.onDraw = function(sender, canvas)
{
canvas.clear(0xff000000);
} // init application and allow fps calculating
app.init(true);
// run application
app.run();