Class: GameObject

GameObject

The core of the engine, all is based on GameObjects They can get renderers, collider (can add more than one but not in an array) You can make groups by adding a GameObject to an other (no recursive limit but be prudent with that, I tried a 5 levels hierarchy and this work perfectly, but you shouldn't have to make a lot)

new GameObject(params)

Parameters:
Name Type Description
params object

All parameters are optional

Properties:
Name Type Argument Default Description
id String <optional>
"0-999999999"

the gameObject id

name String <optional>
"noname"

use name to detect precise type (example player)

tag String <optional>
"none"

use tags for quick type recognition (example characters)

parent GameObject <optional>
null

if you want to set it a parent on creation

childrens Array-GameObject <optional>
[]

if you want to give childs on creation

position Vector2 <optional>

if you give a Vector2, this have to be Vector2 class not a nested object

x Float <optional>
0

x position

y Float <optional>
0

y position

z Float <optional>
0

z position

zindex Int <optional>
0

zindex to order the same z axis objects

renderer Renderer <optional>

give a renderer

renderers Array-Renderer <optional>

give some renderers

collider Collider <optional>
rigidbody RigidBody <optional>

work in progress

Author:
  • Inateno
Source:
Examples
 // the most simple example
var ship = new DE.GameObject();
 // with positions
var ship = new DE.GameObject( { "x": 100, "y": 200, "zindex": 1, "name": "ship", "tag": "player" } );
 // a complete GameObject with a sprite and a CircleCollider
var ship = new DE.GameObject( {
  "x": 100, "y": 200, "zindex": 1, "name": "ship", "tag": "player"
  ,"renderers": [ new DE.SpriteRenderer( { "spriteName": "ship", "offsetY": 100 } ) ]
  ,"collider": new DE.CircleCollider( 60, { "offsetY": 50 } )
} );

Members

<private, static> this.automatism :Object

Type:
  • Object
Source:

<private, static> this.biggerOffset :Sizes

Type:
Source:

<protected, static> this.childrens :Array-GameObject

Type:
  • Array-GameObject
Source:

<protected, static> this.collider :Collider

Type:
Source:

<static> this.enable :Boolean

Type:
  • Boolean
Source:

<protected, static> this.flag :String

Type:
  • String
Source:

<static> this.id :String

Type:
  • String
Source:

<protected, static> this.indexMouseOver :Array

register which cursor is over (used to fire Enter and Leave events)

Type:
  • Array
Source:

<protected, static> this.isMoved :Boolean

register if moved, can be used for collisions, advanced buffers

Type:
  • Boolean
Source:

<private, static> this.killArgs :Object

Type:
  • Object
Source:

<static> this.name :String

Type:
  • String
Source:

<static> this.parent :GameObject

Type:
Source:

<protected, static> this.position :Vector2

Type:
Source:

<protected, static> this.renderers :Array-Renderer

Type:
  • Array-Renderer
Source:

<protected, static> this.rigidbody :RigidBody

Type:
Source:

<protected, static> this.scene :Scene

Type:
Source:

<static> this.tag :String

Type:
  • String
Source:

<static> this.zindex :Int

Type:
  • Int
Source:

Methods

add(object)

add a GameObject as child

Parameters:
Name Type Description
object GameObject
Source:

addAutomatism(id, methodName, params)

This provide you a way to make your custom update / logic
You have to set a name on your automatism (to be able to remove it/change it later) if you provide a name already used you automatism will be overred
- if you set an interval, this automatism will be called each MS given
- if you set persistent to false, it will be removed after the first call
This method call only a protected/public method member of your GameObject

Parameters:
Name Type Argument Description
id String

unique id to be able to remove it later

methodName String

the method to call each time

params Object <optional>

parameters see below

Properties:
Name Type Argument Description
interval Int <optional>

delay between 2 calls

persistent Boolean <optional>

if false, your automatism will be called only once

value1 Undefined <optional>

you can provide a first value

value2 undefined <optional>

you can provide a second value if you provide preventEvents, all kill events will be prevented

Source:
Examples
// this will call myObject.gameLogic() each updates
myObject.addAutomatism( "logic", "gameLogic" );
// this will call myObject.checkInputs() each 500ms
myObject.addAutomatism( "inputs", "checkInputs", { "interval": 500 } );
// this will call myObject.askToKill() in 2.5 seconds
// with parameters preventsEvents = true and will remove itself after
myObject.addAutomatism( "killMeLater", "askToKill", {
  "interval": 2500
  , "value1": { preventEvents: true } // we want prevent the kills events fired when die
  , "persistent": false
} );

addRenderer(renderer)

add given Renderer to this GameObject and return current GameObject instance

Parameters:
Name Type Description
renderer Renderer
Source:
Example
 myObject.addRenderer( new DE.SpriteRenderer( { "spriteName": "ship" } ) );

askToKill(params)

delete the object
this is the good way to destroy an object in the scene
the object is disable and the mainloop will destroy it at the next frame
trigger a kill event with current instance, and call onKill method if provided

Parameters:
Name Type Argument Description
params Object <optional>
Properties:
Name Type Argument Description
preventEvents Boolean <optional>

will prevent all kill events

preventKillEvent Boolean <optional>

will prevent the kill event

preventKilledEvent Boolean <optional>

will prevent the killed event if you provide preventEvents, all kill events will be prevented

Source:
Examples
 myObject.askToKill();
 myObject.askToKill( { preventEvents: true } );

bindGlobalEvent(eventType, isLast)

bind a global event on the scene use this but be sure to declared target function before

Parameters:
Name Type Argument Description
eventType String

Down, Up, Move, case sensitive

isLast Boolean <optional>

if you want this event have to be called after all other of the same type was

Source:

<protected> delete(object)

delete the object

Parameters:
Name Type Description
object GameObject

object reference or object index in the childrens array

Source:

getChildByName(name, recursive)

return the children by name

Parameters:
Name Type Description
name String

name of the GameObject

recursive Boolean

if you want to look in childrens childs

Source:

getHarmonics()

return absolute harmonics (sin, cos)

Source:

getPos()

return absolute position

Source:

getRotation()

return the absolute rotation

Source:

<protected> killMePlease()

this function is called when the mainLoop remove this GameObject (after an askToKill call)
you shouldn't call it directly, if you do it, maybe other GameObjects in the current frame are dealing with this one and should produce errors
if you provided to askToKill a preventEvents or a preventKilledEvent this will not trigger killed event and will not call onKilled method if provided

Source:

lookAt(vector2)

rotate the GameObject to look at the given 2D position

Parameters:
Name Type Description
vector2 Vector2

can be a simple position x-y

Source:

<protected> moved() → {GameObject}

helper to know if an object has moved since last frame (usefull for collisions or custom buffer optimisation)
it's used by the engine but you can use it to, to improve renderings or collisions

Source:
Returns:

current instance

Type
GameObject

onKill()

If you want to get an intern method when your object is killed
usefull when you make your own GameObjects classes, and don't want to inherit all the "on("kill")" events registered
here the list of killed events:
- onKill: when you called askToKill
- onKilled: when your object is deleted (just before we remove collider and childs, so you can still do actions on)

Source:
Examples
 myObject.onKill = function(){ console.log( "I'm dead in 16 milliseconds !" ); };
 myObject.onKilled = function(){ console.log( "Ok, now I'm dead" ); };
 myObject.on( "kill", function( currentObject ){ console.log( "I'm dead in 16 milliseconds !" ); } );
 myObject.on( "killed", function( currentObject ){ console.log( "Ok, now I'm dead" ); } );

onMouseDown(mouse, propagation)

onMouse[type] event, override it with the own GameObject's method
not used as default
mouses events can be triggered only if the current GameObject have a supported Collider (actually, FixedBoxCollider and CircleCollider)
here is the full events list (so override with this name) ordered by call order:
- onMouseDown
- onMouseMove
- onMouseEnter
- onMouseLeave
- onMouseClick
- onMouseUp
you can stop the propagation to the "Last" events, prevent other types, or kill the current GameObject loop, check examples

Parameters:
Name Type Description
mouse MouseEvent

is an engine custom mouse event

propagation PropagationEvent

can kill event that append further current event

Source:
Examples
 // simple event
myObject.onMouseDown = function( event, propagation )
{
  console.log( event, propagation ); // event contain x, y, isDown, index (touchId)
};
 // here I want to catch the Click event, but I want to kill the Up and don't want other GameObject can catch events
so by returning true and stoping propagation, we kill the event
myObject.onMouseClick = function( event, propagation )
{
  // do stuff here
  event.stopPropagation = true; // prevent the camera.onLastMouseClick and all onLastGlobalMouseClick for those who listen Global
  return true; // other GameObjects will not handle the event
};

remove(object)

remove a the given child in this GameObject childrens

Parameters:
Name Type Description
object GameObject

object reference

Source:

removeAutomatism(id)

remove the automatism by id you provided on creation

Parameters:
Name Type Description
id String

automatism id to remove

Source:
Example
// remove the gameLogic previously added
myObject.removeAutomatism( "logic" );

removeAutomatisms()

remove all automatisms

Source:

rotate(angle)

quick access to position.rotate

Parameters:
Name Type Description
angle Float
Source:

<protected> sortChildrens()

sort childrens by zindex and z
engine use this, but you can call it to if you need a to force sort when adding objects for example

Source:

translate(vector2, absolute)

move gameObject with a vector 2

Parameters:
Name Type Description
vector2 Vector2
absolute Boolean

if absolute, object will move on world axis instead this own axis

Source:
Example
 myObject.translate( { "x": 10, "y": 5 }, false );

translateX(distance, absolute)

move gameObject along x axe

Parameters:
Name Type Description
distance Float
absolute Boolean

if absolute, object will move on world axis instead this own axis

Source:

translateY(distance, absolute)

move gameObject along y axe

Parameters:
Name Type Description
distance Float
absolute Boolean

if absolute, object will move on world axis instead this own axis

Source:
Dreamirl Copyright © 2014 And the contributors
Documentation generated by JSDoc 3.2.2 on Thu Apr 24 2014 11:56:41 GMT+0200 (CEST) using the DocStrap template.