Class: GameElement

GameElement(center, children, attrs)

GameElement class. Handles shared actions and properties of game elements

Constructor

new GameElement(center, children, attrs)

Constructor of the GameElement class

Parameters:
Name Type Description
center Point

Absolute position of the element

children Array.<GameDrawable>

Array of drawables

attrs Object

Attribute object

Properties:
Name Type Description
name string

Name of the element. Undefined or unique

level number

Level of the element in relation to its siblings

center Point

Center of the element. Absolute value

homePosition Point

Home position of the element. Element moves here on home() call

children Array.<GameDrawable>

Array of drawables linked to the element

keepOnTop boolean

Keeps the element on top of its siblings

clickable boolean

Element will respond to click

draggable boolean

Element will respond to holding

pressable boolean

Element will respond to key press

stationary boolean

Element will respond to holding, but won't change its position

holdable boolean

Element will respond to holding

onClick Array.<function()>

Array of callbacks called on click

onDrag Array.<function()>

Array of callbacks called on dragging/holding

onFinishDragging Array.<function()>

Array of callbacks called when finished dragging/holding

onStartDragging Array.<function()>

Array of callbacks called when starting dragging/holding

onFinishMouseHold Array.<function()>

Array of callbacks called when finished mouseHold

onKeyPress Object

Map of (keyboard) keys mapped to an array of callbacks called on key press

onKeyHold Object

Map of (keyboard) keys mapped to an array of callbacks called on key hold

onKeyUp Array.<function()>

Array of callbacks called when all keys are lifted

onMove Array.<function()>

Array of callbacks called on move

onMouseHold Array.<Object>

Array of callbacks called on mouse hold

shared Object

Shared object passed from Game

game Game

Reference to the game (passed on game.addElement)

hitboxes Array.<GameHitbox>

Array of hitboxes linked to the element

hitboxVisible boolean

Hitboxes are drawn on true, else are hidden

rotation number

Rotation of element in radians

visible boolean

Element is visible

grid GameGrid

Reference to grid (if it belongs)

animationQueue Array.<Array.<(Point|number)>>

Animations in the future

isAnimating boolean

Flag

Source:

Members

x

Source:

x

Sets X position

Source:

y

Source:

y

Sets Y position

Source:

Methods

addChild(child, sort)

Adds drawable to the children array

Parameters:
Name Type Default Description
child GameDrawable

Added drawable

sort boolean true

Sorts children array on true. False speeds addition but requires sorting later for correct display

Source:

addHitbox(radius, dx, dy)

Creates and adds a new hitbox

Parameters:
Name Type Default Description
radius number

Radius of hitbox

dx number 0

Deviation on X axis

dy number 0

Deviation on Y axis

Source:

addOnClickListener(callback)

Adds a listener to the array of listeners for onClick

Parameters:
Name Type Description
callback function

function to be called

Source:

addOnDragListener(callback)

Adds a listener to the array of listeners for onDrag

Parameters:
Name Type Description
callback function

function to be called

Source:

addOnFinishDraggingListener(callback)

Adds a listener to the array of listeners for onFinishDragging

Parameters:
Name Type Description
callback function

function to be called

Source:

addOnFinishMouseHoldListener(callback)

Adds a listener to the array of listeners for onFinishMouseHold

Parameters:
Name Type Description
callback function

function to be called

Source:

addOnKeyHoldListener(key, callback, stagger)

Adds a listener to the array of listeners for onKeyHold

Parameters:
Name Type Default Description
key string

Key the callback will be called for

callback function

function to be called

stagger number 0

Skips this many calls

Source:

addOnKeyPressListener(key, callback)

Adds a listener to the array of listeners for onKeyPress

Parameters:
Name Type Description
key string

Key the callback will be called for

callback function

function to be called

Source:

addOnKeyUpListener(callback)

Adds a listener to the array of listeners for onKeyUp

Parameters:
Name Type Description
callback function

function to be called

Source:

addOnMouseHoldListener(callback, stagger)

Adds a listener to the array of listeners for mouse hold

Parameters:
Name Type Default Description
callback function

function to be called

stagger number 0

how many calls are skipped (higher = slower execution)

Source:

addOnMoveListener(callback)

Adds a listener to the array of listeners for move

Parameters:
Name Type Description
callback function

function to be called

Source:

addOnStartDraggingListener(callback)

Adds a listener to the array of listeners for onStartDragging

Parameters:
Name Type Description
callback function

function to be called

Source:

animate()

Updates the animation on every child of type GameGif

Source:

animateTo(point, steps)

Animates movement of the element to the point

Parameters:
Name Type Default Description
point Point

Point to animate to

steps number 10

How many steps in animation. Lower = faster

Source:

click(event)

Calls the functions in the onClick array

Parameters:
Name Type Description
event Object

Event passed from listener

Source:

collidesWith(other) → {boolean}

Returns true on collision with the other element

Parameters:
Name Type Description
other GameElement

Element with which the collision is checked

Source:
Returns:

True on colision else false

Type
boolean

copy(newName) → {GameElement}

Returns a new copy of this instance without methods

Parameters:
Name Type Description
newName string

Name of the newly created instance. Names have to be unique.

Source:
Returns:

Copy of this instance

Type
GameElement

createGif(gifName, attrs) → {GameGif}

Creates, adds and returns a new instance of GameGif

Parameters:
Name Type Description
gifName string

Name of gif without extension

attrs Object

Attributes

Source:
Returns:

Newly created instance

Type
GameGif

createImage(imageName, attrs) → {GameImage}

Creates, adds and returns a new instance of GameImage

Parameters:
Name Type Description
imageName string

Name of image WITH extension

attrs Object

Attributes

Source:
Returns:

Newly created instance

Type
GameImage

createShape(type, attrs) → {GameShape}

Creates, adds and returns a new instance of GameShape

Parameters:
Name Type Description
type TYPE

Type of shape (rectangle,oval,line,polygon)

attrs Object

Attributes, line and polygon require coords attribute

Source:
Returns:

Newly created instance

Type
GameShape

createText(text, attrs) → {GameText}

Creates, adds and returns a new instance of GameText

Parameters:
Name Type Description
text string

Text to be displayed

attrs Object

Attributes

Source:
Returns:

Newly created instance

Type
GameText

drag(mousePos, delta, event)

Calls the functions in the onDrag array

Parameters:
Name Type Description
mousePos Point

Mouse position

delta Point

Deviation of the mouse position (when clicked) from the center

event Event
Source:

draw(ctx)

Transforms the canvas context and calls draw function of every visible child

Parameters:
Name Type Description
ctx CanvasRenderingContext2D

Rendering context of the canvas

Source:

finishDragging()

Calls the functions in the onFinishDragging array

Source:

finishMouseHold(event)

Stops and resets mouse hold interval

Parameters:
Name Type Description
event
Source:

getAttrs() → {Object}

Returns an object of attributes (used for copying)

Source:
Returns:

Attribute object

Type
Object

getChildByName(name) → {GameDrawable|GameGif|GameShape|GameText|GameImage}

Returns child drawable with input name or throws an error

Parameters:
Name Type Description
name string

Searched name

Source:
Returns:

Found child drawable

Type
GameDrawable | GameGif | GameShape | GameText | GameImage

home()

Returns element to home position

Source:

isInside(mouse) → {boolean}

Checks if mouse is inside any of the instance's child

Parameters:
Name Type Description
mouse Point

Mouse position

Source:
Returns:

True when inside

Type
boolean

keyHold(key)

Calls the functions in the onKeyHold object that are assigned to the passed keys

Parameters:
Name Type Description
key string

Array of currently pressed keys

Source:

keyPress(key, event)

Calls the functions in the onKeyPress object that are assigned to the passed keys

Parameters:
Name Type Description
key string

Array of currently pressed keys

event Event
Source:

keyUp(event)

Calls the functions in the onKeyUp array and resets stagger values for onKeyHold

Parameters:
Name Type Description
event
Source:

move(delta, triggerOnMove)

Moves the element by vector delta and calls onMove callbacks

Parameters:
Name Type Default Description
delta Point

Vector by which the element is moved

triggerOnMove boolean true

Calls the onMove callbacks on true

Source:

popChildByName(name) → {GameDrawable}

Removes element from elements array and returns it, or throws an error

Parameters:
Name Type Description
name string

Name of element to be removed

Source:
Returns:

Removed element

Type
GameDrawable

removeOnClickListener(callback)

Removes listener for the onClick event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnDragListener(callback)

Removes listener for the onDrag event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnFinishDraggingListener(callback)

Removes listener for the onFinishDragging event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnFinishMouseHoldListener(callback)

Removes listener for the onFinishDragging event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnKeyHoldListener(key, callback)

Removes listener for the onKeyHold event

Parameters:
Name Type Description
key string

Key from which the listener is removed from

callback function

function you want to remove

Source:

removeOnKeyPressListener(key, callback)

Removes listener for the onKeyPress event

Parameters:
Name Type Description
key string

Key from which the listener is removed from

callback function

function you want to remove

Source:

removeOnKeyUpListener(callback)

Removes listener for the onKeyUp event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnMouseHoldListener(callback)

Removes listener for mouse hold

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnMoveListener(callback)

Removes listener for the onClick event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

removeOnStartDraggingListener(callback)

Removes listener for the onStartDragging event

Parameters:
Name Type Description
callback function

function you want to remove

Source:

setHome(x, y)

Sets home position

Parameters:
Name Type Description
x number
y number
Source:

setPosition(x, y)

Changes the position of element to values

Parameters:
Name Type Description
x number

X coordinate

y number

Y coordinate

Source:

sortChildren()

Sorts children array by level

Source:

startDragging()

Calls the functions in the onStartDragging array

Source:

startMouseHold(event)

Creates mouse hold interval

Parameters:
Name Type Description
event
Source: