Game Class
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();
Item Index
Methods
Properties
Attributes
Methods
add
obj
Add sprite to the game.
Parameters:
-
obj
SpriteThe instance of Sprite class which should be added to the game
beforeInit
() protected
Sanity check before init of game object instance.
draw
canvas
Implements the drawing of the game.
Parameters:
-
canvas
CanvasThe 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:
-
[transition=null]
SceneTransition optionalInstance of SceneTransition classes: (SlideToBottom, SlideToTop, SlideToLeft, SlideToRight), which defines transition effect
quit
() chainable
Quit application.
Example:
// hardware key reaction
game.onKeyPressed = function(keyCode)
{
if (keyCode == #back || keyCode == #home)
game.quit();
}
run
allowFPS=false
Function starts a game. It creates a game window and an events managment.
Parameters:
-
allowFPS=false
BooleanEnable 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:
-
enable
Booleanenable gesture
Example:
this.setGesturePan(true);
setGesturePinch
enable
Enable and set gesture pinch. Pinch gesture is commonly used to resize photos or zoom the screen.
Parameters:
-
enable
Booleanenable gesture
setGesturePress
enable
numberOfTapsRequired
numberOfTouchesRequired
Enable and set press gesture. Developer can specify number of taps and touches required to recognize the gesture.
Parameters:
Example:
this.setGesturePress(true, 0, 1);
setGestureRotate
enable
Enable and set rotate gesture. The rotate gesture enable to rotate screen by more fingers.
Parameters:
-
enable
Booleanenable gesture
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.
setGestureTap
enable
numberOfTapsRequired
numberOfTouchesRequired
Enable and tap gesture. Developer can specify number of taps and touches required to recognize the gesture.
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
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
IntegerPressed key value
onDraw
OnDraw event appears every time, when it is needed to redraw application window.
Event Payload:
-
canvas
CanvasThe 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
SymbolThe 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
FloattranslationX Movement on x axis from the previous call of this event
-
transY
FloattranslationY Movement on y axis from the previous call of this event
-
velX
FloatvelocityX Velocity on x axis from the previous call of this event
-
velY
FloatvelocityY 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
SymbolThe 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
FloatScale from the previous call of this event
-
velocity
Floatvelocity 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
SymbolThe 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
FloatRotation from the previous call of this event
-
velocity
Floatvelocity 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
SymbolThe 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 | CharPressed 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 | CharPressed 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
SymbolNew 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.
onPointerPressed
This event is raised by user tap the screen.
onPointerReleased
Called when touch (click) is finished.
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());
}