Class: RuntimeObject

RuntimeObject

new gdjs.RuntimeObject(runtimeScene, objectData)

RuntimeObject represents an object being used on a RuntimeScene.

The constructor can be called on an already existing RuntimeObject: In this case, the constructor will try to reuse as much already existing members as possible (recycling).

However, you should not be calling the constructor on an already existing object which is not a RuntimeObject.

A gdjs.RuntimeObject should not be instantiated directly, always a child class (because gdjs.RuntimeObject don't call onCreated at the end of its constructor).

Name Type Description
runtimeScene gdjs.RuntimeScene

The gdjs.RuntimeScene the object belongs to.

objectData ObjectData

The initial properties of the object.

Members

protectedaabbAABB

protectedanglenumber

protectedhiddenboolean

protectedhitBoxesArray.<gdjs.Polygon>

protectedhitBoxesDirtyboolean

protectedidnumber

protectedlayerstring

protectednamestring

protectedtypestring

protectedxnumber

protectedynumber

protectedzOrdernumber

Methods

staticgdjs.RuntimeObject.collisionTest(obj1, obj2, ignoreTouchingEdges){boolean}

Return true if the hitboxes of two objects are overlapping

Name Type Description
obj1 gdjs.RuntimeObject

The first runtimeObject

obj2 gdjs.RuntimeObject

The second runtimeObject

ignoreTouchingEdges boolean | undefined

If true, then edges that are touching each other, without the hitbox polygons actually overlapping, won't be considered in collision.

Returns:
Type Description
boolean true if obj1 and obj2 are in collision

staticgdjs.RuntimeObject.distanceTest()

Check the distance between two objects.

staticgdjs.RuntimeObject.getNameIdentifier()

Get the identifier associated to an object name. Some features may want to compare objects name a large number of time. In this case, it may be more efficient to compare objects name identifiers.

staticgdjs.RuntimeObject.getVariableChildCount(variable)

Get the number of children from a variable

Name Type Description
variable

The variable to be accessed

Returns:
The number of children

staticgdjs.RuntimeObject.getVariableNumber(variable){number}

Get the value of a variable considered as a number. Equivalent of variable.getAsNumber()

Name Type Description
variable gdjs.Variable

The variable to be accessed

Returns:
Type Description
number The value of the specified variable

staticgdjs.RuntimeObject.getVariableString(variable)

Get the value of a variable considered as a string. Equivalent of variable.getAsString()

Name Type Description
variable

The variable to be accessed

Returns:
The string of the specified variable

staticgdjs.RuntimeObject.returnVariable(variable)

Return the variable passed as argument without any change. Only for usage by events.

Name Type Description
variable gdjs.Variable

The variable to be accessed

Returns:
The specified variable

staticgdjs.RuntimeObject.setVariableNumber(variable, newValue)

Shortcut to set the value of a variable considered as a number

Name Type Description
variable

The variable to be changed

newValue number

The value to be set

staticgdjs.RuntimeObject.setVariableString(variable, newValue)

Shortcut to set the value of a variable considered as a string

Name Type Description
variable

The variable to be changed

newValue String

The value to be set

activateBehavior(name, enable)

De/activate a behavior of the object.

Name Type Description
name String

The behavior name.

enable boolean

true to activate the behavior

addForce(x, y, multiplier)

Add a force to the object to move it.

Name Type Description
x number

The x coordinates of the force

y number

The y coordinates of the force

multiplier number

Set the force multiplier

addForceTowardObject(object, len, multiplier)

Add a force oriented toward another object.
(Shortcut for addForceTowardPosition)

Name Type Description
object gdjs.RuntimeObject

The target object

len number

The force length, in pixels.

multiplier number

Set the force multiplier

addForceTowardPosition(x, y, len, multiplier)

Add a force oriented toward a position

Name Type Description
x number

The target x position

y number

The target y position

len number

The force length, in pixels.

multiplier number

Set the force multiplier

addPolarForce(angle, len, multiplier)

Add a force using polar coordinates.

Name Type Description
angle number

The angle of the force, in degrees.

len number

The length of the force, in pixels.

multiplier number

Set the force multiplier

averageForceAngleIs(angle, toleranceInDegrees){boolean}

Return true if the average angle of the forces applied on the object is in a given range.

Name Type Description
angle number

The angle to be tested.

toleranceInDegrees number

The length of the range :

Returns:
Type Description
boolean true if the difference between the average angle of the forces and the angle parameter is inferior to toleranceInDegrees parameter.

behaviorActivated(name)

Check if a behavior is activated

Name Type Description
name String

The behavior name.

Returns:
true if the behavior is activated.

clearForces()

Deletes all forces applied on the object

cursorOnObject()

Return true if the cursor, or any touch, is on the object.

Returns:
true if the cursor, or any touch, is on the object.

deleteFromScene(runtimeScene)

Remove an object from a scene.

Do not change/redefine this method. Instead, redefine the onDestroyFromScene method.

Name Type Description
runtimeScene gdjs.RuntimeScene

The RuntimeScene owning the object.

extraInitializationFromInitialInstance(initialInstanceData)

Called when the object is created from an initial instance at the startup of the scene.
Note that common properties (position, angle, z order...) have already been setup.

Name Type Description
initialInstanceData

The data of the initial instance.

Get the AABB (axis aligned bounding box) for the object.

The default implementation uses either the position/size of the object (when angle is 0) or hitboxes (when angle is not 0) to compute the bounding box. Result is cached until invalidated (by a position change, angle change...).

You should probably redefine updateAABB instead of this function.

Returns:
Type Description
AABB The bounding box (example: {min: [10,5], max:[20,10]})

getAngle(){number}

Get the rotation of the object.

Returns:
Type Description
number The rotation of the object, in degrees.

getAverageForce(){gdjs.Force}

Return a force which is the sum of all forces applied on the object.

Returns:
Type Description
gdjs.Force A force object.

Get a behavior from its name.

Be careful, the behavior must exists, no check is made on the name.

Name Type Description
name String

The behavior name.

Returns:
Type Description
gdjs.RuntimeBehavior The behavior with the given name, or undefined.

getCenterX(){number}

Return the X position of the object center, relative to the object X position (getDrawableX).

Returns:
Type Description
number the X position of the object center, relative to getDrawableX().

getCenterY(){number}

Return the Y position of the object center, relative to the object position (getDrawableY).

Returns:
Type Description
number the Y position of the object center, relative to getDrawableY().

getDistanceToObject(otherObject)

Get the distance, in pixels, between the center of this object and another object.

Name Type Description
otherObject gdjs.RuntimeObject

The other object

getDrawableX(){number}

Get the X position of the rendered object.

For most objects, this will returns the same value as getX(). But if the object has an origin that is not the same as the point (0,0) of the object displayed, getDrawableX will differ.

Returns:
Type Description
number The X position of the rendered object.

getDrawableY(){number}

Get the Y position of the rendered object.

For most objects, this will returns the same value as getY(). But if the object has an origin that is not the same as the point (0,0) of the object displayed, getDrawableY will differ.

Returns:
Type Description
number The Y position of the rendered object.

getElapsedTime(runtimeScene)

Return the time elapsed since the last frame, in milliseconds, for the object.

Objects can have different elapsed time if they are on layers with different time scales.

Name Type Description
runtimeScene gdjs.RuntimeScene

The RuntimeScene the object belongs to.

getHeight(){number}

Return the width of the object.

Returns:
Type Description
number The height of the object

getHitBoxes(){Array.<gdjs.Polygon>}

Get the hit boxes for the object.
The default implementation returns a basic bouding box based the size (getWidth and getHeight) and the center point of the object (getCenterX and getCenterY).

You should probably redefine updateHitBoxes instead of this function.

Returns:
Type Description
Array.<gdjs.Polygon> An array composed of polygon.

getLayer(){string}

Get the layer of the object.

Returns:
Type Description
string The layer of the object

getName(){string}

Get the name of the object.

Returns:
Type Description
string The object's name.

getNameId(){number}

Get the name identifier of the object.

Returns:
Type Description
number The object's name identifier.

getRendererObject(){Object}

Called with a callback function that should be called with the internal object used for rendering by the object (PIXI.DisplayObject...)

Returns:
Type Description
Object The internal rendered object (PIXI.DisplayObject...)

getSqDistanceTo(pointX, pointY)

Get the squared distance, in pixels, from the object center to a position.

Name Type Description
pointX number

X position

pointY number

Y position

getSqDistanceToObject(otherObject)

Get the squared distance, in pixels, between the center of this object and another object.

Name Type Description
otherObject gdjs.RuntimeObject

The other object

getTimerElapsedTimeInSeconds(timerName){number}

Get a timer elapsed time.

Name Type Description
timerName String

The timer name

Returns:
Type Description
number The timer elapsed time in seconds, 0 if the timer doesn't exist

getUniqueId(){number}

Get the unique identifier of the object.
The identifier is set by the runtimeScene owning the object.
You can also use the id property (this._object.id) for increased efficiency instead of calling this method.

Returns:
Type Description
number The object identifier

Get the container of the object variables

Returns:
Type Description
gdjs.VariablesContainer The variables of the object

getVisibilityAABB(){AABB}

Get the AABB (axis aligned bounding box) to be used to determine if the object is visible on screen. The gdjs.RuntimeScene will hide the renderer object if the object is not visible on screen ("culling").

The default implementation uses the AABB returned by getAABB.

If null is returned, the object is assumed to be always visible.

Returns:
Type Description
AABB The bounding box (example: {min: [10,5], max:[20,10]}) or null.

getWidth(){number}

Return the width of the object.

Returns:
Type Description
number The width of the object

getX(){number}

Get the X position of the object.

Returns:
Type Description
number The X position of the object

getY(){number}

Get the Y position of the object.

Returns:
Type Description
number The Y position of the object

getZOrder(){number}

Get the Z order of the object.

Returns:
Type Description
number The Z order of the object

hasBehavior(name)

Check if a behavior is used by the object.

Name Type Description
name String

The behavior name.

hasNoForces(){boolean}

Return true if no forces are applied on the object.

Returns:
Type Description
boolean true if no forces are applied on the object.

hasVariable(name){boolean}

Shortcut to test if a variable exists for the object.

Name Type Description
name string

The variable to be tested

Returns:
Type Description
boolean true if the variable exists.

hide(enable)

Hide (or show) the object.

Name Type Description
enable boolean

Set it to true to hide the object, false to show it.

insideObject()

Return true if the specified position is inside object bounding box.

The position should be in "world" coordinates, i.e use gdjs.Layer.convertCoords if you need to pass the mouse or a touch position that you get from gdjs.InputManager.

isCollidingWithPoint(pointX, pointY)

Check if a point is inside the object collision hitboxes.

Name Type Description
pointX

The point x coordinate.

pointY

The point y coordinate.

Returns:
true if the point is inside the object collision hitboxes.

isHidden(){boolean}

Return true if the object is hidden.

Returns:
Type Description
boolean true if the object is hidden.

isOnLayer(layer){boolean}

Return true if the object is on the specified layer

Name Type Description
layer string

The layer to be tested.

Returns:
Type Description
boolean true if the object is on the specified layer

isVisible(){boolean}

Return true if the object is not hidden.

Note: This is unrelated to the actual visibility of the objec on the screen. For this, see getVisibilityAABB to get the bounding boxes of the object as displayed on the scene.

Returns:
Type Description
boolean true if the object is not hidden.

onCreated()

To be called by the child classes in their constructor, at the very end. Notify the behaviors that they have been constructed (this must be done when the object is ready, otherwise behaviors can do operations on the object which could be not initialized yet).

If you redefine this function, make sure to call the original method (gdjs.RuntimeObject.prototype.onCreated.call(this);).

onDestroyFromScene(runtimeScene)

Called when the object is destroyed (because it is removed from a scene or the scene is being unloaded). If you redefine this function, make sure to call the original method (gdjs.RuntimeObject.prototype.onDestroyFromScene.call(this, runtimeScene);).

Name Type Description
runtimeScene gdjs.RuntimeScene

The scene owning the object.

pauseTimer(timerName)

Pause a timer, if the timer doesn't exist it is created

Name Type Description
timerName String

The timer name

putAround(x, y, distance, angleInDegrees)

Put the object around a position, with a specific distance and angle. The distance and angle are computed between the position and the center of the object.

Name Type Description
x number

The x position of the target

y number

The y position of the target

distance number

The distance between the object and the target, in pixels.

angleInDegrees number

The angle between the object and the target, in degrees.

putAroundObject(obj, distance, angleInDegrees)

Put the object around another object, with a specific distance and angle. The distance and angle are computed between the centers of the objects.

Name Type Description
obj

The target object

distance number

The distance between the object and the target

angleInDegrees number

The angle between the object and the target, in degrees.

raycastTest(x, y, endX, endY, closest)

Name Type Description
x number

The raycast source X

y number

The raycast source Y

endX number

The raycast end position X

endY number

The raycast end position Y

closest boolean

Get the closest or farthest collision mask result?

Returns:
A raycast result with the contact points and distances

removeTimer(timerName)

Remove a timer

Name Type Description
timerName String

The timer name

resetTimer(timerName)

Reset a timer, if the timer doesn't exist it is created

Name Type Description
timerName String

The timer name

rotate(speed, runtimeScene)

Rotate the object at the given speed

Name Type Description
speed number

The speed, in degrees per second.

runtimeScene gdjs.RuntimeScene

The scene where the object is displayed.

separateFromObjects(objects, ignoreTouchingEdges)

Separate the object from others objects, using their hitboxes.

Name Type Description
objects

Objects

ignoreTouchingEdges boolean | undefined

If true, then edges that are touching each other, without the hitbox polygons actually overlapping, won't be considered in collision.

Returns:
true if the object was moved

separateFromObjectsList(objectsLists, ignoreTouchingEdges)

Separate the object from others objects, using their hitboxes.

Name Type Description
objectsLists

Tables of objects

ignoreTouchingEdges boolean | undefined

If true, then edges that are touching each other, without the hitbox polygons actually overlapping, won't be considered in collision.

Returns:
true if the object was moved

separateObjectsWithForces(objectsLists)

Name Type Description
objectsLists

Tables of objects

Deprecated
  • Yes

separateObjectsWithoutForces(objectsLists)

Name Type Description
objectsLists

Tables of objects

Deprecated
  • Yes

setAngle(angle)

Set the angle of the object.

Name Type Description
angle number

The new angle of the object

setLayer(layer)

Set the layer of the object.

Name Type Description
layer string

The new layer of the object

setPosition(x, y)

Set the position of the object.

Name Type Description
x number

The new X position

y number

The new Y position

Set the X position of the object.

Name Type Description
x number

The new X position

Set the Y position of the object.

Name Type Description
y number

The new Y position

setZOrder(z)

Set the Z order of the object.

Name Type Description
z number

The new Z order position of the object

stepBehaviorsPostEvents()

Call each behavior stepPostEvents method.

stepBehaviorsPreEvents()

Call each behavior stepPreEvents method.

timerElapsedTime(timerName, timeInSeconds){boolean}

Test a timer elapsed time, if the timer doesn't exist it is created

Name Type Description
timerName String

The timer name

timeInSeconds number

The time value to check in seconds

Returns:
Type Description
boolean True if the timer exists and its value is greater than or equal than the given time, false otherwise

timerPaused(timerName){boolean}

Test a if a timer is paused

Name Type Description
timerName String

The timer name

Returns:
Type Description
boolean True if the timer exists and is paused, false otherwise

unpauseTimer(timerName)

Unpause a timer, if the timer doesn't exist it is created

Name Type Description
timerName String

The timer name

update(runtimeScene)

Called once during the game loop, before events and rendering.

Name Type Description
runtimeScene gdjs.RuntimeScene

The gdjs.RuntimeScene the object belongs to.

updateAABB()

Update the AABB (axis aligned bounding box) for the object.

Default implementation uses either the position/size of the object (when angle is 0) or hitboxes (when angle is not 0) to compute the bounding box.

You should not call this function by yourself, it is called when necessary by getAABB method. However, you can redefine it if your object can have a faster implementation.

updateForces()

Called once a step by runtimeScene to update forces magnitudes and remove null ones.

updateHitBoxes()

Update the hit boxes for the object.

The default implementation set a basic bounding box based on the size (getWidth and getHeight) and the center point of the object (getCenterX and getCenterY). Result is cached until invalidated (by a position change, angle change...).

You should not call this function by yourself, it is called when necessary by getHitBoxes method. However, you can redefine it if your object need custom hit boxes.

updateTimers(elapsedTime)

Updates the object timers. Called once during the game loop, before events and rendering.

Name Type Description
elapsedTime number

The elapsed time since the previous frame in milliseconds.