Class: World

Phaser. World

Phaser.World

new World(game)

"This world is but a canvas to our imagination." - Henry David Thoreau

A game has only one world. The world is an abstract place in which all game objects live. It is not bound by stage limits and can be any size. You look into the world via cameras. All game objects live within the world at world-based coordinates. By default a world is created the same size as your Stage.

Parameters:
Name Type Description
game Phaser.Game

Reference to the current game instance.

Source:

Extends

Members

alive

Properties:
Name Type Description
alive boolean

The alive property is useful for Groups that are children of other Groups and need to be included/excluded in checks like forEachAlive.

Inherited From:
Default Value:
  • true
Source:

alpha

Properties:
Name Type Description
alpha number

The alpha value of the Group container.

Inherited From:
Source:

angle

The angle of rotation of the Group container. This will adjust the Group container itself by modifying its rotation. This will have no impact on the rotation value of its children, but it will update their worldTransform and on-screen position.

Properties:
Name Type Description
angle number

The angle of rotation given in degrees, where 0 degrees = to the right.

Inherited From:
Source:

bounds

The World has no fixed size, but it does have a bounds outside of which objects are no longer considered as being "in world" and you should use this to clean-up the display list and purge dead objects. By default we set the Bounds to be from 0,0 to Game.width,Game.height. I.e. it will match the size given to the game constructor with 0,0 representing the top-left of the display. However 0,0 is actually the center of the world, and if you rotate or scale the world all of that will happen from 0,0. So if you want to make a game in which the world itself will rotate you should adjust the bounds so that 0,0 is the center point, i.e. set them to -1000,-1000,2000,2000 for a 2000x2000 sized world centered around 0,0.

Properties:
Name Type Description
bounds Phaser.Rectangle

Bound of this world that objects can not escape from.

Source:

camera

Properties:
Name Type Description
camera Phaser.Camera

Camera instance.

Source:

<readonly> centerX

Properties:
Name Type Description
centerX number

Gets the X position corresponding to the center point of the world.

Source:

<readonly> centerY

Properties:
Name Type Description
centerY number

Gets the Y position corresponding to the center point of the world.

Source:

currentRenderOrderID

Properties:
Name Type Description
currentRenderOrderID number

Reset each frame, keeps a count of the total number of objects updated.

Source:

cursor

The cursor is a simple way to iterate through the objects in a Group using the Group.next and Group.previous functions. The cursor is set to the first child added to the Group and doesn't change unless you call next, previous or set it directly with Group.cursor.

Properties:
Name Type Description
cursor any

The current display object that the Group cursor is pointing to.

Inherited From:
Source:

exists

Properties:
Name Type Description
exists boolean

If exists is true the the Group is updated, otherwise it is skipped.

Inherited From:
Default Value:
  • true
Source:

game

Properties:
Name Type Description
game Phaser.Game

A reference to the currently running Game.

Inherited From:
Source:

group

Properties:
Name Type Description
group Phaser.Group

The parent Group of this Group, if a child of another.

Inherited From:
Source:

height

Properties:
Name Type Description
height number

Gets or sets the current height of the game world.

Source:

<readonly> length

Properties:
Name Type Description
length number

The total number of children in this Group, regardless of their exists/alive status.

Inherited From:
Source:

name

Properties:
Name Type Description
name string

A name for this Group. Not used internally but useful for debugging.

Inherited From:
Source:

pivot

Properties:
Name Type Description
pivot Phaser.Point

The pivot point of the Group container.

Inherited From:
Source:

<readonly> randomX

Properties:
Name Type Description
randomX number

Gets a random integer which is lesser than or equal to the current width of the game world.

Source:

<readonly> randomY

Properties:
Name Type Description
randomY number

Gets a random integer which is lesser than or equal to the current height of the game world.

Source:

rotation

The angle of rotation of the Group container. This will adjust the Group container itself by modifying its rotation. This will have no impact on the rotation value of its children, but it will update their worldTransform and on-screen position.

Properties:
Name Type Description
rotation number

The angle of rotation given in radians.

Inherited From:
Source:

scale

Properties:
Name Type Description
scale Phaser.Point

The scane of the Group container.

Inherited From:
Source:

<readonly> total

Properties:
Name Type Description
total number

The total number of children in this Group who have a state of exists = true.

Inherited From:
Source:

<protected> type

Properties:
Name Type Description
type number

Internal Phaser Type value.

Inherited From:
Source:

visible

Properties:
Name Type Description
visible boolean

Gets or sets the visible state of the World.

Source:

width

Properties:
Name Type Description
width number

Gets or sets the current width of the game world.

Source:

x

The x coordinate of the Group container. You can adjust the Group container itself by modifying its coordinates. This will have no impact on the x/y coordinates of its children, but it will update their worldTransform and on-screen position.

Properties:
Name Type Description
x number

The x coordinate of the Group container.

Inherited From:
Source:

y

The y coordinate of the Group container. You can adjust the Group container itself by modifying its coordinates. This will have no impact on the x/y coordinates of its children, but it will update their worldTransform and on-screen position.

Properties:
Name Type Description
y number

The y coordinate of the Group container.

Inherited From:
Source:

Methods

add(child) → {*}

Adds an existing object to this Group. The object can be an instance of Phaser.Sprite, Phaser.Button or any other display object. The child is automatically added to the top of the Group, so renders on-top of everything else within the Group. If you need to control that then see the addAt method.

Parameters:
Name Type Description
child *

An instance of Phaser.Sprite, Phaser.Button or any other display object..

Inherited From:
Source:
See:
Returns:

The child that was added to the Group.

Type
*

addAll(property, amount, checkAlive, checkVisible)

Adds the amount to the given property on all children in this Group. Group.addAll('x', 10) will add 10 to the child.x value.

Parameters:
Name Type Description
property string

The property to increment, for example 'body.velocity.x' or 'angle'.

amount number

The amount to increment the property by. If child.x = 10 then addAll('x', 40) would make child.x = 50.

checkAlive boolean

If true the property will only be changed if the child is alive.

checkVisible boolean

If true the property will only be changed if the child is visible.

Inherited From:
Source:

addAt(child, index) → {*}

Adds an existing object to this Group. The object can be an instance of Phaser.Sprite, Phaser.Button or any other display object. The child is added to the Group at the location specified by the index value, this allows you to control child ordering.

Parameters:
Name Type Description
child *

An instance of Phaser.Sprite, Phaser.Button or any other display object..

index number

The index within the Group to insert the child to.

Inherited From:
Source:
Returns:

The child that was added to the Group.

Type
*

<protected> boot()

Initialises the game world.

Source:

bringToTop(child) → {*}

Brings the given child to the top of this Group so it renders above all other children.

Parameters:
Name Type Description
child *

The child to bring to the top of this Group.

Inherited From:
Source:
Returns:

The child that was moved.

Type
*

callAll(method, context, parameter)

Calls a function on all of the children regardless if they are dead or alive (see callAllExists if you need control over that) After the method parameter and context you can add as many extra parameters as you like, which will all be passed to the child.

Parameters:
Name Type Argument Default Description
method string

A string containing the name of the function that will be called. The function must exist on the child.

context string <optional>
null

A string containing the context under which the method will be executed. Set to null to default to the child.

parameter * <repeatable>

Additional parameters that will be passed to the method.

Inherited From:
Source:

callAllExists(callback, existsValue, parameter)

Calls a function on all of the children that have exists=true in this Group. After the existsValue parameter you can add as many parameters as you like, which will all be passed to the child callback.

Parameters:
Name Type Argument Description
callback function

The function that exists on the children that will be called.

existsValue boolean

Only children with exists=existsValue will be called.

parameter * <repeatable>

Additional parameters that will be passed to the callback.

Inherited From:
Source:

<protected> callbackFromArray(child, callback, length)

Returns a reference to a function that exists on a child of the Group based on the given callback array.

Parameters:
Name Type Description
child object

The object to inspect.

callback array

The array of function names.

length number

The size of the array (pre-calculated in callAll).

Inherited From:
Source:

childTest()

Internal test.

Inherited From:
Source:

countDead() → {number}

Call this function to find out how many members of the group are dead.

Inherited From:
Source:
Returns:

The number of children flagged as dead.

Type
number

countLiving() → {number}

Call this function to find out how many members of the group are alive.

Inherited From:
Source:
Returns:

The number of children flagged as alive.

Type
number

create(x, y, key, frame, exists) → {Phaser.Sprite}

Automatically creates a new Phaser.Sprite object and adds it to the top of this Group. Useful if you don't need to create the Sprite instances before-hand.

Parameters:
Name Type Argument Default Description
x number

The x coordinate to display the newly created Sprite at. The value is in relation to the Group.x point.

y number

The y coordinate to display the newly created Sprite at. The value is in relation to the Group.y point.

key string

The Game.cache key of the image that this Sprite will use.

frame number | string <optional>

If the Sprite image contains multiple frames you can specify which one to use here.

exists boolean <optional>
true

The default exists state of the Sprite.

Inherited From:
Source:
Returns:

The child that was created.

Type
Phaser.Sprite

createMultiple(quantity, key, frame, exists)

Automatically creates multiple Phaser.Sprite objects and adds them to the top of this Group. Useful if you need to quickly generate a pool of identical sprites, such as bullets. By default the sprites will be set to not exist and will be positioned at 0, 0 (relative to the Group.x/y)

Parameters:
Name Type Argument Default Description
quantity number

The number of Sprites to create.

key string

The Game.cache key of the image that this Sprite will use.

frame number | string <optional>

If the Sprite image contains multiple frames you can specify which one to use here.

exists boolean <optional>
false

The default exists state of the Sprite.

Inherited From:
Source:

destroy()

Destroyer of worlds.

Source:

divideAll(property, amount, checkAlive, checkVisible)

Divides the given property by the amount on all children in this Group. Group.divideAll('x', 2) will half the child.x value.

Parameters:
Name Type Description
property string

The property to divide, for example 'body.velocity.x' or 'angle'.

amount number

The amount to divide the property by. If child.x = 100 then divideAll('x', 2) would make child.x = 50.

checkAlive boolean

If true the property will only be changed if the child is alive.

checkVisible boolean

If true the property will only be changed if the child is visible.

Inherited From:
Source:

forEach(callback, callbackContext, checkExists)

Allows you to call your own function on each member of this Group. You must pass the callback and context in which it will run. After the checkExists parameter you can add as many parameters as you like, which will all be passed to the callback along with the child. For example: Group.forEach(awardBonusGold, this, true, 100, 500) Note: Currently this will skip any children which are Groups themselves.

Parameters:
Name Type Description
callback function

The function that will be called. Each child of the Group will be passed to it as its first parameter.

callbackContext Object

The context in which the function should be called (usually 'this').

checkExists boolean

If set only children with exists=true will be passed to the callback, otherwise all children will be passed.

Inherited From:
Source:

forEachAlive(callback, callbackContext)

Allows you to call your own function on each alive member of this Group (where child.alive=true). You must pass the callback and context in which it will run. You can add as many parameters as you like, which will all be passed to the callback along with the child. For example: Group.forEachAlive(causeDamage, this, 500)

Parameters:
Name Type Description
callback function

The function that will be called. Each child of the Group will be passed to it as its first parameter.

callbackContext Object

The context in which the function should be called (usually 'this').

Inherited From:
Source:

forEachAlive(callback, callbackContext)

Allows you to call your own function on each alive member of this Group (where child.alive=true). You must pass the callback and context in which it will run. You can add as many parameters as you like, which will all be passed to the callback along with the child. For example: Group.forEachAlive(causeDamage, this, 500)

Parameters:
Name Type Description
callback function

The function that will be called. Each child of the Group will be passed to it as its first parameter.

callbackContext Object

The context in which the function should be called (usually 'this').

Inherited From:
Source:

forEachDead(callback, callbackContext)

Allows you to call your own function on each dead member of this Group (where alive=false). You must pass the callback and context in which it will run. You can add as many parameters as you like, which will all be passed to the callback along with the child. For example: Group.forEachDead(bringToLife, this)

Parameters:
Name Type Description
callback function

The function that will be called. Each child of the Group will be passed to it as its first parameter.

callbackContext Object

The context in which the function should be called (usually 'this').

Inherited From:
Source:

getAt(index) → {*}

Returns the child found at the given index within this Group.

Parameters:
Name Type Description
index number

The index to return the child from.

Inherited From:
Source:
Returns:

The child that was found at the given index.

Type
*

getFirstAlive() → {Any}

Call this function to retrieve the first object with alive === true in the group. This is handy for checking if everything has been wiped out, or choosing a squad leader, etc.

Inherited From:
Source:
Returns:

The first alive child, or null if none found.

Type
Any

getFirstDead() → {Any}

Call this function to retrieve the first object with alive === false in the group. This is handy for checking if everything has been wiped out, or choosing a squad leader, etc.

Inherited From:
Source:
Returns:

The first dead child, or null if none found.

Type
Any

getFirstExists(state) → {Any}

Call this function to retrieve the first object with exists == (the given state) in the Group.

Parameters:
Name Type Description
state boolean

True or false.

Inherited From:
Source:
Returns:

The first child, or null if none found.

Type
Any

getIndex(child) → {number}

Get the index position of the given child in this Group.

Parameters:
Name Type Description
child *

The child to get the index for.

Inherited From:
Source:
Returns:

The index of the child or -1 if it's not a member of this Group.

Type
number

getRandom(startIndex, length) → {Any}

Returns a member at random from the group.

Parameters:
Name Type Description
startIndex number

Optional offset off the front of the array. Default value is 0, or the beginning of the array.

length number

Optional restriction on the number of values you want to randomly select from.

Inherited From:
Source:
Returns:

A random child of this Group.

Type
Any

iterate(key, value, returnType, callback, callbackContext) → {any}

Iterates over the children of the Group. When a child has a property matching key that equals the given value, it is considered as a match. Matched children can be sent to the optional callback, or simply returned or counted. You can add as many callback parameters as you like, which will all be passed to the callback along with the child, after the callbackContext parameter.

Parameters:
Name Type Argument Default Description
key string

The child property to check, i.e. 'exists', 'alive', 'health'

value any

If child.key === this value it will be considered a match. Note that a strict comparison is used.

returnType number

How to return the data from this method. Either Phaser.Group.RETURN_NONE, Phaser.Group.RETURN_TOTAL or Phaser.Group.RETURN_CHILD.

callback function <optional>
null

Optional function that will be called on each matching child. Each child of the Group will be passed to it as its first parameter.

callbackContext Object <optional>

The context in which the function should be called (usually 'this').

Inherited From:
Source:
Returns:

Returns either a numeric total (if RETURN_TOTAL was specified) or the child object.

Type
any

multiplyAll(property, amount, checkAlive, checkVisible)

Multiplies the given property by the amount on all children in this Group. Group.multiplyAll('x', 2) will x2 the child.x value.

Parameters:
Name Type Description
property string

The property to multiply, for example 'body.velocity.x' or 'angle'.

amount number

The amount to multiply the property by. If child.x = 10 then multiplyAll('x', 2) would make child.x = 20.

checkAlive boolean

If true the property will only be changed if the child is alive.

checkVisible boolean

If true the property will only be changed if the child is visible.

Inherited From:
Source:

next()

Advances the Group cursor to the next object in the Group. If it's at the end of the Group it wraps around to the first object.

Inherited From:
Source:

postUpdate()

This is called automatically before the renderer runs and after the plugins have updated. In postUpdate this is where all the final physics calculatations and object positioning happens. The objects are processed in the order of the display list. The only exception to this is if the camera is following an object, in which case that is updated first.

Source:

previous()

Moves the Group cursor to the previous object in the Group. If it's at the start of the Group it wraps around to the last object.

Inherited From:
Source:

remove(child) → {boolean}

Removes the given child from this Group and sets its group property to null.

Parameters:
Name Type Description
child Any

The child to remove.

Inherited From:
Source:
Returns:

true if the child was removed from this Group, otherwise false.

Type
boolean

removeAll()

Removes all children from this Group, setting all group properties to null. The Group container remains on the display list.

Inherited From:
Source:

removeBetween(startIndex, endIndex)

Removes all children from this Group whos index falls beteen the given startIndex and endIndex values.

Parameters:
Name Type Description
startIndex number

The index to start removing children from.

endIndex number

The index to stop removing children from. Must be higher than startIndex and less than the length of the Group.

Inherited From:
Source:

replace(oldChild, newChild)

Replaces a child of this Group with the given newChild. The newChild cannot be a member of this Group.

Parameters:
Name Type Description
oldChild *

The child in this Group that will be replaced.

newChild *

The child to be inserted into this group.

Inherited From:
Source:

set(child, key, value, checkAlive, checkVisible, operation)

This function allows you to quickly set a property on a single child of this Group to a new value. The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.

Parameters:
Name Type Argument Default Description
child Phaser.Sprite

The child to set the property on.

key string

The property, as a string, to be set. For example: 'body.velocity.x'

value *

The value that will be set.

checkAlive boolean <optional>
false

If set then the child will only be updated if alive=true.

checkVisible boolean <optional>
false

If set then the child will only be updated if visible=true.

operation number <optional>
0

Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.

Inherited From:
Source:

setAll(key, value, checkAlive, checkVisible, operation)

This function allows you to quickly set the same property across all children of this Group to a new value. The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.

Parameters:
Name Type Argument Default Description
key string

The property, as a string, to be set. For example: 'body.velocity.x'

value *

The value that will be set.

checkAlive boolean <optional>
false

If set then only children with alive=true will be updated.

checkVisible boolean <optional>
false

If set then only children with visible=true will be updated.

operation number <optional>
0

Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.

Inherited From:
Source:

setBounds(x, y, width, height)

Updates the size of this world. Note that this doesn't modify the world x/y coordinates, just the width and height.

Parameters:
Name Type Description
x number

Top left most corner of the world.

y number

Top left most corner of the world.

width number

New width of the world. Can never be smaller than the Game.width.

height number

New height of the world. Can never be smaller than the Game.height.

Source:

setProperty(child, key, value, operation)

Sets the given property to the given value on the child. The operation controls the assignment of the value.

Parameters:
Name Type Argument Default Description
child *

The child to set the property value on.

key array

An array of strings that make up the property that will be set.

value *

The value that will be set.

operation number <optional>
0

Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.

Inherited From:
Source:

sort(index, order)

Call this function to sort the group according to a particular value and order. For example to depth sort Sprites for Zelda-style game you might call group.sort('y', Phaser.Group.SORT_ASCENDING) at the bottom of your State.update().

Parameters:
Name Type Argument Default Description
index string <optional>
'y'

The string name of the property you want to sort on.

order number <optional>
Phaser.Group.SORT_ASCENDING

The Group constant that defines the sort order. Possible values are Phaser.Group.SORT_ASCENDING and Phaser.Group.SORT_DESCENDING.

Inherited From:
Source:

subAll(property, amount, checkAlive, checkVisible)

Subtracts the amount from the given property on all children in this Group. Group.subAll('x', 10) will minus 10 from the child.x value.

Parameters:
Name Type Description
property string

The property to decrement, for example 'body.velocity.x' or 'angle'.

amount number

The amount to subtract from the property. If child.x = 50 then subAll('x', 40) would make child.x = 10.

checkAlive boolean

If true the property will only be changed if the child is alive.

checkVisible boolean

If true the property will only be changed if the child is visible.

Inherited From:
Source:

swap(child1, child2) → {boolean}

Swaps the position of two children in this Group. Both children must be in this Group. You cannot swap a child with itself, or swap un-parented children, doing so will return false.

Parameters:
Name Type Description
child1 *

The first child to swap.

child2 *

The second child to swap.

Inherited From:
Source:
Returns:

True if the swap was successful, otherwise false.

Type
boolean

swapIndex()

Internal test.

Inherited From:
Source:

update()

This is called automatically after the plugins preUpdate and before the State.update. Most objects have preUpdate methods and it's where initial movement, drawing and calculations are done.

Source:

update()

This is called automatically after the State.update, but before particles or plugins update. Most objects won't have an update method set unless explicitly given one.

Source:
Phaser Copyright © 2012-2014 Photon Storm Ltd.
Documentation generated by JSDoc 3.3.0-dev on Wed Feb 05 2014 06:28:37 GMT-0000 (GMT) using the DocStrap template.