Moscrif API Docs for: 2012q3
Show:

Game Class

include "lib://game2d/game.ms";
Library: game2d

Game class is a base class for all Moscrif game projects, which use framework. This class map all system events like onStart, onDraw, onPointerPressed etc. It provides correct working of other framework elements (provides its drawing, reaction onto user events etc).

Example:

include "lib://game2d/game.ms"

// create new instances of game and resource classes
var game = new Game();

game.onStart = function()
{
    ...
}

// hardware key reaction
game.onKeyPressed = function(keyCode)
{
    if (keyCode == #back || keyCode == #home)
        game.quit();
}

game.run();

Methods

_setTransition

(
  • transition
)
private

Sets transition.

Parameters:

add

(
  • obj
)

Add sprite to the game.

Parameters:

  • obj Sprite

    The instance of Sprite class which should be added to the game

beforeInit

() protected

Sanity check before init of game object instance.

draw

(
  • canvas
)
protected

Implements the drawing of the game.

Parameters:

  • canvas Canvas

    The canvas used to draw graphics

init

() protected

Init game object instance. This method is called from class constructor and it is usually no needed to change it.

invalidate

() chainable

Enforce redraw window. By default a game is redraw many time per second, what meant that it is no need to enforce window redraw.

pop

(
  • [transition=null]
)

Return to previous scene. Scenes could be changed with a transition effect.

Parameters:

quit

() chainable

Quit application.

Example:

// hardware key reaction
game.onKeyPressed = function(keyCode)
{
    if (keyCode == #back || keyCode == #home)
        game.quit();
}

run

(
  • allowFPS=false
)
chainable

Function starts a game. It creates a game window and an events managment.

Parameters:

  • allowFPS=false Boolean

    Enable or disable FPS calculating.

Example:

include "lib://game2d/game.ms"

// create new instances of game and resource classes
var game = new Game();

game.onStart = function()
{
    ...
}

...

game.run();

setGesturePan

(
  • enable
)

Enable and pan gesture.

Parameters:

Example:

this.setGesturePan(true);

setGesturePinch

(
  • enable
)

Enable and set gesture pinch. Pinch gesture is commonly used to resize photos or zoom the screen.

Parameters:

setGesturePress

(
  • enable
  • numberOfTapsRequired
  • numberOfTouchesRequired
)

Enable and set press gesture. Developer can specify number of taps and touches required to recognize the gesture.

Parameters:

  • enable Boolean

    enable gesture

  • numberOfTapsRequired Integer

    number of required taps

  • numberOfTouchesRequired Integer

    number of required touches

Example:

this.setGesturePress(true, 0, 1);

setGestureRotate

(
  • enable
)

Enable and set rotate gesture. The rotate gesture enable to rotate screen by more fingers.

Parameters:

Example:

this.setGestureRotate(true);

setGestureSwipe

(
  • enable
  • numberOfTouchesRequired
  • direction
)

Enable gesture swipe and set supported direction and required number of touches. By this gesture user user can execute a command by swiping his finger across the screen.

Parameters:

  • enable Boolean

    Enables or disables gestures.

  • numberOfTouchesRequired Integer

    Required number of touches

  • direction Symbol

    Supported directions are #left, #right, #up, #down or #all

setGestureTap

(
  • enable
  • numberOfTapsRequired
  • numberOfTouchesRequired
)

Enable and tap gesture. Developer can specify number of taps and touches required to recognize the gesture.

Parameters:

  • enable Boolean

    Enables or disables gestures.

  • numberOfTapsRequired Integer

    required number of taps

  • numberOfTouchesRequired Integer

    required number of touches

start

() protected

Default method mapped onto onStart event.

Properties

multiTouch

Boolean

Enable / disable multi touch. If multi touch is enabled the pointer events has last parameter -> index (order of touch);

orientation

Symbol

This property sets supported device orientations. If there are more supported orientations event onOrientationChanged is called, when device orientation change. Supported are:

  • #portrait
  • #landscape-left
  • #landscape-right

Attributes

instance

Game static

Singleton of the game object.

Events

onBackground

This event is raised when game moves to background. An application moves to background when:

  • Application moves to hibernation state
  • Other application is activated
  • In the case of system calls (incoming call or displaying other system dialog or window)

Example:

game.onBackground = function()
{
    musicPlayer.stop();
}

onChar

This method is called when game moves to background. An application moves to foreground when:

  • Application starts
  • Application is selected from Task Manager
  • Application is restored form hibernation
  • Application becomes active after system calls (incoming call etc …) finish

Event Payload:

  • charCode Integer

    Pressed key value

onDraw

OnDraw event appears every time, when it is needed to redraw application window.

Event Payload:

  • canvas Canvas

    The instance of Canvas class, used to draw a graphics onto screen

Example:

game.onDraw = function(canvas)
{
    // fill screen with blue color
    canvas.clear(0xff0000ff);
}

onForeground

This method is called when game moves to background. An application moves to foreground when:

  • Application starts
  • Application is selected from Task Manager
  • Application is restored form hibernation
  • Application becomes active after system calls (incoming call etc …) finish

Example:

game.onForeground = function()
{
    musicPlayer.play();
}

onGesturePan

This event is raised by pan gesture.

Event Payload:

  • state Symbol

    The following symbols are used:

    • #began: the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    • #chnaged: the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    • #ended: the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    • #cancelled: the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
  • transX Float

    translationX Movement on x axis from the previous call of this event

  • transY Float

    translationY Movement on y axis from the previous call of this event

  • velX Float

    velocityX Velocity on x axis from the previous call of this event

  • velY Float

    velocityY Velocity on y axis from the previous call of this event

Example:

game.onGesturePan = function(state, transX, transY, velX, velY) { // translate photo this.translateX -= transX; this.translateY -= transY; }

onGesturePinch

This event is raised by pinch gesture.

Event Payload:

  • state Symbol

    The following symbols are used:

    • #began: the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    • #chnaged: the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    • #ended: the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    • #cancelled: the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
  • scale Float

    Scale from the previous call of this event

  • velocity Float

    velocity from the previous call of this event

Example:

// manage gestures
game.onGesturePinch = function(state, scale, velocity)
{

    this._scale = this._scale * scale;
    if (this._scale < this._minScale)
        this._scale = this._minScale;
    else if (this._scale > this._maxScale)
        this._scale = this._maxScale;
}

onGesturePress

This event is raised by press gesture. Number of reguired taps and touches can be customized by set setGesturePress method.

Example:

game.onGesturePress = function()
{

    this.gesturePinch(true);
    this.gestureRotate(true);
    this.gesturePan(false);
    this.action = #scale;
}

onGestureRotate

This event is raised by rotate gesture.

Event Payload:

  • state Symbol

    The following symbols are used:

    • #began: the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    • #chnaged: the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    • #ended: the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    • #cancelled: the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
  • angle Float

    Rotation from the previous call of this event

  • velocity Float

    velocity from the previous call of this event

Example:

game.onGestureRotate = function(state, angle, velocity)
{
    this.state = state;
    if (state != #ended) {
        // radians to degrees
        this._angle -= this.lastRotation - angle * 180.0/ Math.PI;
        this.lastRotation = angle  * 180.0/ Math.PI;
        this.rotating = true;
    } else {
        this.lastRotation = 0;
        this.rotating = false;
    }
}

onGestureSwipe

This event is raised by swipe gesture. By this gesture user user can execute a command by swiping his finger across the screen.

Event Payload:

  • direction Symbol

    The Direction: #left, #right, #up or #down.

onGestureTap

This event is raised by tap gesture. Number of reguired taps and touches can be customized by set setGestureTap method.

onKeyPressed

This event is raised by press the key.

Event Payload:

  • key Symbol | Char

    Pressed key value (symbols #back for back arrow, #home for home button and #menu for menu button)

Example:

// reaction to hardware keyboard press
game.onKeyPressed = function(keyCode)
{
    if (keyCode == #back || keyCode == #home)
        app.quit();
}

onKeyReleased

This event is raised by release the key.

Event Payload:

  • key Symbol | Char

    Pressed key value (symbols #back for back arrow, #home for home button and #menu for menu button)@return Function

onOrientationChanged

This event is raised by change device orientation.

Event Payload:

  • orientation Symbol

    New orientation of device

Example:

game.onOrientationChanged = function(orientation)
{
    // resize all photos to current orientation
    logI("new orientation is : ", orientation);
}

onPointerDragged

Called when user moves his finger on the screen.

Event Payload:

  • x Integer

    X coordinates of pointer

  • y Integer

    Y coordinates of pointer

  • pointer Integer

    Order of touch if multi-touch is enabled

onPointerPressed

This event is raised by user tap the screen.

Event Payload:

  • x Integer

    X coordinates of pointer

  • y Integer

    Y coordinates of pointer

  • pointer Integer

    Order of touch if multi-touch is enabled

onPointerReleased

Called when touch (click) is finished.

Event Payload:

  • x Integer

    X coordinates of pointer

  • y Integer

    Y coordinates of pointer

  • pointer Integer

    Order of touch if multi-touch is enabled

onProcess

On process event appears at every about 25 miliseconds, and is a good place for asynchronous operations. F.e.: it is good place to remove physics elements from physics world, or check changes in source data etc.

onStart

OnStart event appears, when application starts. It is called only once after start of application.

Example:

// create new instances of game and resource classes
var game = new Game();

game.onStart = function()
{
    // create a scene, whre whole game is situated
    this.push(new GameScene());
}