Skip to content

View.GameObject.API

Jesper Sørensen edited this page Sep 7, 2014 · 1 revision

Class: GameObject

GameObject

A class which incorporates functions that are often used by objects in games: - Is drawn as a sprite - Has movement vector - Has collision checking

new GameObject(source, x, y, direction, additionalProperties)

The constructor for the GameObject class.

Parameters:
Name Type Argument Default Description
source string

A string representing the source of the object's bitmap

x number <optional>
0

The x-position of the object in the game arena, in pixels

y number <optional>
0

The y-position of the object in the game arena, in pixels

direction number <optional>
0

The rotation (in radians) of the object when drawn in the game arena

additionalProperties Object <optional>

An object containing additional properties to assign to the created object. The default is: { size: 1, opacity: 1, composite: 'source-over', offset: new Math.Vector('center', 'center'), loop: 'eachFrame', speed: new Math.Vector(0, 0) }

Properties:
Name Type Description
loop Engine.CustomLoop

The loop to which movement of the object has been assigned

alive boolean

Whether or not the object is alive. If the object is not alive, it will not move

speed Math.Vector

The two-directional velocity of the object in px/second

Source:

Extends

Members

drawCanvas

Remove drawCanvas function which was inherited from View

Inherited From:
Source:

Methods

addChildren(child1, child2) → {View.Child[]}(child1, child2) → {View.Child[]}

Adds children to a View object. If the object that the children are added to, is a descendant of the current room, the children will be drawn on the stage when added. The added children will be drawn above the current children.

Parameters:
Name Type Description
child1 View.Child

A child to add to the View object

child2 View.Child

Another child to add...

Inherited From:
Source:
Returns:

An array containing the added children

Type
View.Child[]

animate(properties, options)(properties, options)(properties, options)

Used for animating numeric properties of the owner of the function. Available easing functions are: "linear" "quadIn" "quadOut" "quadInOut" "powerIn" "powerOut" "powerInOut" "sinusInOut"

Parameters:
Name Type Description
properties object

An object containing key-value pairs in the following format: { "[property name]": "[end value]" }

options object

An object containing key-value pairs for the animation's option: { "duraton": "[animation duration (in ms)]", "callback": "[callback function]", "easing": "[easing function to use]" }

Inherited From:
Source:

applyToThisAndChildren(func)(func)

Executes a function for the View and all of the its children.

Parameters:
Name Type Description
func function

Function to execute

Inherited From:
Source:

boundingBoxCollidesWith(objects, getCollidingObjects) → {Array.<Object>|boolean}(objects, getCollidingObjects) → {Array.<Object>|boolean}

Checks for a collision with other objects' rotated BBoxes. The word polygon is used because the check is actually done by using the Polygon object.

Parameters:
Name Type Description
objects Collidable | Array.<Collidable>

Target object, or array of target objects

getCollidingObjects boolean

Whether or not to return an array of colliding objects (is slower because the check cannot stop when a single collission has been detected)

Inherited From:
Source:
Returns:

If getCollidingObjects is set to true, an array of colliding object, else a boolean representing whether or not a collission was detected.

Type
Array.<Object> | boolean

ChildInitWithoutRedrawRegions()()()

Inherited From:
Source:

collidesWith(objects, getCollisionPosition, getCollidingObjects) → {Object|boolean}(objects, getCollisionPosition, getCollidingObjects) → {Object|boolean}

A "metafunction" for checking if the Collidable collides with another object of the same type. This function uses boundingBoxCollidesWith for narrowing down the number of objects to check, then uses maskCollidesWith for doing a precise collision check on the remaining objects.

Parameters:
Name Type Description
objects View.Collidable | Array.<View.Collidable>

Target object, or array of target objects

getCollisionPosition boolean

If true, the function returns an object representing the position of the detected collision. Defaults to false

getCollidingObjects boolean

If true, the function returns all colliding objects

Inherited From:
Source:
Returns:

If not getCollisionPosition or getCollidingObjects is true, a boolean representing whether or not a collision was detected. If getCollisionPosition and or getCollidingObjects is true, returns an object of the following type: { "objects": [Array of colliding objects], "positions": [Array of collision positions for each object] "combinedPosition": [The combined position of the collision] }

                     If getCollidingObjects is false, the objects-array will be empty and the positions-array will only contain one position which is the average collision position for all colliding objects.
If getCollisionPosition is false, the positions-array will be empty
If both getCollisionPosition and getCollidingObjects are true, the objects-array will contain all colliding objects, and the positions-array will contain each colliding object's collision position
Type
Object | boolean

draw(c, area, forceRedraw)(c, area, forceRedraw)

Draws all children and grandchildren of an object that inherits the View class. It is usually not necessary to call this function since it is automatically called by the engine's redraw loop.

Parameters:
Name Type Description
c CanvasRenderingContext2D

A canvas' 2d context to draw the children on

area Math.Rectangle

A rectangle specifying the area to draw

forceRedraw boolean

Whether or not to force a redraw even though draw caching is enabled (this option is actually used when caching the view)

Inherited From:
Source:

getAnimations() → {Object[]}() → {Object[]}() → {Object[]}

Fetches all current animations of the object.

Inherited From:
Source:
Returns:

An array of all the current animations of the object

Type
Object[]

getChildren() → {View.Child[]}() → {View.Child[]}

Fetches an array of all the View's children. This will not return a pointer, so changing the returned array will not change the View's children.

Inherited From:
Source:
Returns:

Array containing all of the View's children

Type
View.Child[]

getCombinedRedrawRegion() → {Math.Rectangle}() → {Math.Rectangle}

Gets the complete region that will used for drawing on next redraw

Inherited From:
Source:
Returns:

A rectangle representing the region

Type
Math.Rectangle

getDirectionTo(child) → {number}(child) → {number}(child) → {number}

Calculates the direction to another child

Parameters:
Name Type Description
child View.Child

The object to calculate the direction to

Inherited From:
Source:
Returns:

The direction in radians

Type
number

getDistanceTo(child) → {number}(child) → {number}(child) → {number}

Calculates the distance to another child (the distance between the object's positions)

Parameters:
Name Type Description
child View.Child

The object to calculate the distance to

Inherited From:
Source:
Returns:

The distance in pixels

Type
number

getParents() → {View.Container[]}() → {View.Container[]}() → {View.Container[]}

Creates and returns and array of all the child's parents (from closest to farthest)

Inherited From:
Source:
Returns:

A list of all the child's parents

Type
View.Container[]

<private> getRedrawRegion() → {Rectangle}() → {Rectangle}

Calculates the region which the sprite will fill out when redrawn.

Inherited From:
Source:
Returns:

The bounding rectangle of the sprite's redraw

Type
Rectangle

getRoom() → {View.Room|Boolean}() → {View.Room|Boolean}() → {View.Room|Boolean}

Finds the room to which the object is currently added

Inherited From:
Source:
Returns:

The room to which the object is currently added, or false if the object is not added to a room

Type
View.Room | Boolean

getRoomPosition() → {Math.Vector|Boolean}() → {Math.Vector|Boolean}() → {Math.Vector|Boolean}

Fetches the position of the child inside the room

Inherited From:
Source:
Returns:

The objects position in its room, or false if the object is not placed in any room.

Type
Math.Vector | Boolean

getTheme() → {string}() → {string}

Fetches the name of the theme which currently applies to the object.

Inherited From:
Source:
Returns:

The name of the object's theme

Type
string

insertBelow(insertChildren, child) → {View.Child[]}(insertChildren, child) → {View.Child[]}

Adds a child to a View object, below an already added child. This means that the inserted child (or children) will be drawn below the child which they are inserted below.

Parameters:
Name Type Description
insertChildren View.Child | Array.<View.Child>

Child or array of children to insert before an existing child

child View.Child

Current child to insert other children before

Inherited From:
Source:
Returns:

Array of the inserted children

Type
View.Child[]

isAnimated() → {boolean}() → {boolean}() → {boolean}

Checks if the object is currently being animated.

Inherited From:
Source:
Returns:

Whether or not the object is being animated

Type
boolean

isDrawn() → {Boolean}() → {Boolean}() → {Boolean}

Checks if the child object is in a state where it will get drawn. For this function to return true, the child object has to be both visible and placed in a visible room.

Inherited From:
Source:
Returns:

Whether or not the child object is in a state where it will get drawn

Type
Boolean

isInVisibleRoom() → {Boolean}() → {Boolean}() → {Boolean}

Checks if the child object is inside a room that is currently visible

Inherited From:
Source:
Returns:

Whether or not the child object is currently in a visible room

Type
Boolean

isVisible() → {boolean}() → {boolean}() → {boolean}

Checks if the objects is visible. This function runs before each draw to ensure that it is necessary

Inherited From:
Source:
Returns:

Whether or not the object is visible (based on its size and opacity vars)

Type
boolean

maskCollidesWith(objects, getCollisionPosition) → {Object|boolean}(objects, getCollisionPosition) → {Object|boolean}

Checks for a mask based collisions with other Collidable objects.

Parameters:
Name Type Description
objects View.Collidable | Array.<View.Collidable>

Target object, or array of target objects

getCollisionPosition boolean

If true, the function returns an object representing the position of the detected collision. Defaults to false

Inherited From:
Source:
Returns:

If getCollisionPosition is false, a boolean representing whether or not a collision was detected, else an object of the following type: { "x": [The average horizontal distance from the Collidable to the detected collision], "y": [The average vertical distance from the Collidable to the detected collision], "pixelCount": [The number of colliding pixels] }

Type
Object | boolean

moveTo(x, y)(x, y)(x, y)

Sets the position of the object relative to its parent

Parameters:
Name Type Description
x number

The horisontal position

y number

The vertical position

Inherited From:
Source:

parseOffsetGlobal(offset) → {Math.Vector}(offset) → {Math.Vector}

Parses an offset global into an actual Math.Vector offset that fits the instance

Parameters:
Name Type Description
offset number

Offset global (OFFSET_TOP_LEFT, etc.)

Inherited From:
Source:
Returns:

The offset vector the offset global corresponds to for the instance

Type
Math.Vector

propertyIsAnimated() → {boolean}() → {boolean}() → {boolean}

Checks if a specific property is current being animated

Inherited From:
Source:
Returns:

Whether or not the property is being animated

Type
boolean

<private> refreshSource()()

Updates the source bitmap of the object to correspond to it's current theme (set with setTheme or inherited). Calling this function is usually not necessary since it is automatically called when setting the theme of the object itself or it's parent object.

Inherited From:
Source:

removeAllChildren(purge)(purge)

Removes all children from the View.

Parameters:
Name Type Description
purge boolean

Whether or not to purge the removed children, meaning that their scheduled functions and loop-attached functions will be removed. (true by default)

Inherited From:
Source:

removeChildren(child1, child2) → {View.Child[]}(child1, child2) → {View.Child[]}

Removes one or more children from the View.

Parameters:
Name Type Description
child1 View.Child

A child to add to the View object

child2 View.Child

Another child to remove...

Inherited From:
Source:
Returns:

An array of the children which was removed. If an object, which was supplied as argument, was not a child of the View, it will not appear in the returned array

Type
View.Child[]

setSource(source)(source)

Sets the bitmap-source of the object. For more information about bitmaps and themes, see themes.

Parameters:
Name Type Description
source string

The resource string of the bitmap-source to use for the object

Inherited From:
Source:

setTheme(themeName, recursive)(themeName, recursive)

Sets theme of an View. Children whose theme is not already set, will inherit the set theme. To enforce the theme to all children, use the recursive argument.

Parameters:
Name Type Argument Default Description
themeName string

The name of the theme to apply as the object's theme

recursive boolean <optional>
false

Whether or not the set theme will be applied to children for which a theme has already been set. If this argument is unset, it will default to false

Inherited From:
Source:

stopAnimations()()()

Stops all current animations of the object.

Inherited From:
Source:

<private> updatePosition()

Adds the game object's speed vector to its current position. This function is automatically run in each frame.

Source:

Generated with wicked.
Clone this wiki locally