API Docs for: v0.1.0
Show:

Tilelayer Class

The Tilelayer is the visual tiled layer that actually displays on the screen

This class will be created by the Tilemap, there shouldn't be a reason to create an instance on your own.

Constructor

Tilelayer

(
  • map
  • layer
)

Parameters:

  • map Tilemap

    The tilemap instance that this belongs to

  • layer Object

    All the settings for the layer

Methods

_freeTile

(
  • tx
  • ty
)
private

Frees a tile in the list back into the pool

Parameters:

  • tx Number

    The x-coord of the tile in tile coords (not world coords)

  • ty Number

    The y-coord of the tile in tile coords (not world coords)

_getInteractive

(
  • set
  • props
)
Boolean private

Checks if an object should be marked as interactive

Parameters:

  • set Tileset

    The tileset for the object

  • props Object

    The Tiled properties object

Returns:

Boolean:

Whether or not the item is interactive

_preRender

() private

Renders the map onto different canvases, one per chunk. This only runs once then the canvases are used as a textures for tiles the size of chunks.

_preRenderChunk

(
  • cx
  • cy
  • w
  • h
)
private

Renders a single chunk to a single canvas and creates/places the tile instance for it.

Parameters:

  • cx Number

    The x-coord of this chunk's top left

  • cy Number

    The y-coord of this chunk's top left

  • w Number

    The width of this chunk

  • h Number

    The height of this chunk

_renderDown

(
  • [forceNew=false]
)
private

Renders tiles to the bottom, pulling from the far top

Parameters:

  • [forceNew=false] Boolean optional

    If set to true, new tiles are created instead of trying to recycle

_renderLeft

(
  • [forceNew=false]
)
private

Renders tiles to the left, pulling from the far right

Parameters:

  • [forceNew=false] Boolean optional

    If set to true, new tiles are created instead of trying to recycle

_renderRight

(
  • [forceNew=false]
)
private

Renders tiles to the right, pulling from the far left

Parameters:

  • [forceNew=false] Boolean optional

    If set to true, new tiles are created instead of trying to recycle

_renderTiles

(
  • sx
  • sy
  • sw
  • sh
)
private

Renders the tiles for the viewport

Parameters:

  • sx Number

    The x-coord in the map to start rendering

  • sy Number

    The y-coord in the map to start rendering

  • sw Number

    The width of the viewport

  • sh Number

    The height of the viewport

_renderUp

(
  • [forceNew=false]
)
private

Renders tiles to the top, pulling from the far bottom

Parameters:

  • [forceNew=false] Boolean optional

    If set to true, new tiles are created instead of trying to recycle

addChild

(
  • child
)
Container | Sprite

Adds a child to the container and returns the child

Parameters:

Returns:

Container | Sprite:

The child that was added

addChildAt

(
  • child
  • index
)
Container | Sprite

Adds a child to the object at a specified index. If the index is out of bounds an error will be thrown

Parameters:

Returns:

Container | Sprite:

The child that was added

bringChildToTop

(
  • child
)
Container | Sprite

Brings a child to the top of the Z pile.

Parameters:

Returns:

Container | Sprite:

The child that was added

clearTile

(
  • tile
  • remove
)
Tilelayer chainable

Clears a tile currently used to render the layer

Parameters:

  • tile Tile

    The tile object to clear

  • remove Boolean

    Should this tile be completely removed (never to bee seen again)

Returns:

Tilelayer:

Returns itself.

clearTiles

(
  • remove
)
Tilelayer chainable

Clears all the tiles currently used to render the layer

Parameters:

  • remove Boolean

    Should this tile be completely removed (never to bee seen again)

Returns:

Tilelayer:

Returns itself.

destroy

()

Inherited from Container but overwritten in src\tilemap\Tilelayer.js:668

Destroys the tile layer completely

destroyAllChildren

() Container chainable

Destroys all the children of the object.

Returns:

Container:

Returns itself.

disablePhysics

() Mixed chainable async

Inherited from PhysicsTarget but overwritten in src\physics\PhysicsTarget.js:83

Disbales physics for this sprite

Returns:

Mixed:

Returns itself.

emit

(
  • type
  • data
)
Mixed chainable

Emits an event which will run all registered listeners for the event type

Parameters:

  • type String

    The event name to emit

  • data Mixed

    Any data you want passed along with the event

Returns:

Mixed:

Returns itself.

enablePhysics

(
  • system
)
Mixed chainable async

Enables physics for this sprite

Parameters:

Returns:

Mixed:

Returns itself.

hide

() Container chainable

Sets the container to visible = false

Returns:

Container:

Returns itself.

moveTileSprite

(
  • fromTileX
  • fromTileY
  • toTileX
  • toTileY
)
Tile

Moves a tile sprite from one position to another, and creates a new tile if the old position didn't have a sprite

Parameters:

  • fromTileX Number

    The x coord of the tile in units of tiles (not pixels) to move from

  • fromTileY Number

    The y coord of the tile in units of tiles (not pixels) to move from

  • toTileX Number

    The x coord of the tile in units of tiles (not pixels) to move to

  • toTileY Number

    The y coord of the tile in units of tiles (not pixels) to move to

Returns:

Tile:

The sprite to display

off

(
  • type
  • listener
)
Mixed chainable

Removes a listener function for an event type

Parameters:

  • type String

    The event name to emit

  • listener Function

    The function to remove

Returns:

Mixed:

Returns itself.

on

(
  • type
  • listener
)
Mixed chainable

Registers a listener function to be run on an event occurance

Parameters:

  • type String

    The event name to listen for

  • listener Function

    The function to execute when the event happens

Returns:

Mixed:

Returns itself.

once

(
  • type
  • listener
)
Mixed chainable

Registers a one-time callback for an event

Parameters:

  • type String

    The event name to listen for

  • listener Function

    the callback to call when the event occurs

Returns:

Mixed:

Returns itself.

onCollide

(
  • obj
  • collision
)
Boolean

Called whenever a collision occurs

Parameters:

  • obj Sprite | Container

    The sprite that you collide with

  • collision Collision

    Collision data object that contains information about the intersection

Returns:

Boolean:

If you return false explicitly the engine will not solve the collision. In this way you can make thing "not collide" if you don't want their types to collide.

onTileEvent

(
  • eventName
  • tile
  • data
)
private

Called whenever a tile event occurs, this is used to echo to the parent.

Parameters:

  • eventName String

    The name of the event

  • tile Tile

    The tile the event happened to

  • data Mixed

    The event data that was passed along

pan

(
  • dx
  • dy
)
Tilelayer chainable

Pans the layer around, rendering stuff if necessary

Parameters:

  • dx Number | Point

    The x amount to pan, if a Point is passed the dy param is ignored

  • dy Number

    The y ammount to pan

Returns:

Tilelayer:

Returns itself.

removeAllChildren

() Container chainable

Removes all children from the object.

Returns:

Container:

Returns itself.

removeChild

(
  • child
)
Container | Sprite

Removes a child from the object.

Parameters:

Returns:

Container | Sprite:

The child that was added

resize

(
  • width
  • height
)
Tilelayer chainable

Creates all the tile sprites needed to display the layer

Parameters:

  • width Number

    The number of tiles in the X direction to render

  • height Number

    The number of tiles in the Y direction to render

Returns:

Tilelayer:

Returns itself.

setMass

(
  • mass
)
Mixed chainable

Sets the mass of this sprite

Parameters:

  • mass Number

    The new mass of the object

Returns:

Mixed:

Returns itself.

setPosition

(
  • x
  • y
)
Mixed chainable

Sets the position of this sprite

Parameters:

  • x Number
  • y Number

Returns:

Mixed:

Returns itself.

setRotation

(
  • rotation
)
Mixed chainable

Sets the rotation of this sprite

Parameters:

  • rotation Number

    The new rotation of the object in radians

Returns:

Mixed:

Returns itself.

setVelocity

(
  • velocity
)
Mixed chainable

Sets the velocity of this sprite

Parameters:

  • velocity Vector

    The new velocity of the object

Returns:

Mixed:

Returns itself.

show

() Container chainable

Sets the container to visible = true

Returns:

Container:

Returns itself.

Properties

_phys

Object private

The physics namespace that all physics properties go into. Those properties are:

  • system {PhysicsSystem} PhysicsSystem that this object is a part of.
  • active {Boolean} Whether or not this target is actively having physics simulated.

Default: {}

alpha

Number

The opacity of the object.

children

Array

[read-only] The of children of this object.

chunkSize

Vector

The size of a chunk when pre rendering

Default: new Vector(512, 512)

hitArea

Rectangle | Polygon | Circle | Ellipse

This is the defined area that will pick up mouse / touch events. It is null by default. Setting it is a neat way of optimising the hitTest function that the interactionManager will use (as it will not need to hit test all the children)

inertia

Number

The moment of inertia of this object, only set this before enabling physics (has no effect after enabling)

Default: 0

interactive

Boolean

Wether or not the object will handle mouse events

Default: false

map

Tilemap

The map instance this tilelayer belongs to

mass

Number

The mass of this object, please use setMass to set this value

Default: 0

name

String

The name of the layer

Default: ''

parent

DisplayObject

[read-only] The display object that contains this display object.

position

Point

The coordinate of the object relative to the local coordinates of the parent.

preRender

Boolean

Is this layer supposed to be preRendered?

Default: false

properties

Object

The user-defined properties of this group. Usually defined in the TiledEditor

rotation

Number

The rotation of the object in radians.

scale

Point

The scale factor of the object.

size

Vector

The size of the layer

Default: new Vector(1, 1)

stage

Stage

[read-only] The stage the display object is connected to, or undefined if it is not connected to the stage.

state

Game

The state instance this tilelayer belongs to

state

Game

The state instance this tilelayer belongs to

tileIds

Uint32Array

The tile IDs of the tilemap

tiles

Object

The current map of all tiles on the screen

type

String

The Tiled type of tile layer, should always be 'tilelayer'

Default: 'tilelayer'

visible

Boolean

The visibility of the object.

Events

click

A callback that is used when the users clicks on the displayObject with their mouse

Event Payload:

  • interactionData InteractionData

collision

On Collision Event called when this sprite collides into another, or is being collided into by another. By default if something collides with a collectable sprite we destroy the collectable and if we collide with a solid tile we kill our velocity. This method will emit a 'collision' event that you can listen for

Event Payload:

  • obj Sprite

    Colliding sprite

  • vec Vector

    Collision vector (for sensors this is normalized)

  • colShape cp.Shape

    The colliding physics shape

  • myShape cp.Shape

    Your physics shape that caused the collision

mousedown

A callback that is used when the user clicks the mouse down over the displayObject

Event Payload:

  • interactionData InteractionData

mousemove

A callback that is used when the user moves the mouse while over the displayObject

Event Payload:

  • interactionData InteractionData

mouseout

A callback that is used when the users mouse leaves the displayObject

Event Payload:

  • interactionData InteractionData

mouseover

A callback that is used when the users mouse rolls over the displayObject

Event Payload:

  • interactionData InteractionData

mouseup

A callback that is used when the user releases the mouse that was over the displayObject for this callback to be fired the mouse must have been pressed down over the displayObject

Event Payload:

  • interactionData InteractionData

mouseupoutside

A callback that is used when the user releases the mouse that was over the displayObject but is no longer over the displayObject for this callback to be fired, The touch must have started over the displayObject

Event Payload:

  • interactionData InteractionData

separate

On Seperate Event called when this sprite collides into another, or is being collided into by another. By default if something collides with a collectable sprite we destroy the collectable and if we collide with a solid tile we kill our velocity. This method will emit a 'collision' event that you can listen for

Event Payload:

  • obj Sprite

    Colliding sprite

  • colShape cp.Shape

    The colliding physics shape

  • myShape cp.Shape

    Your physics shape that caused the collision

tap

A callback that is used when the users taps on the displayObject with their finger basically a touch version of click

Event Payload:

  • interactionData InteractionData

touchend

A callback that is used when the user releases a touch over the displayObject

Event Payload:

  • interactionData InteractionData

touchendoutside

A callback that is used when the user releases the touch that was over the displayObject for this callback to be fired, The touch must have started over the displayObject

Event Payload:

  • interactionData InteractionData

touchstart

A callback that is used when the user touch's over the displayObject

Event Payload:

  • interactionData InteractionData