A Graphics object is a way to draw primitives to your game. Primitives include forms of geometry, such as Rectangles,
Circles and Polygons. They also include lines, arcs and curves. When you initially create a Graphics object it will
be empty. To 'draw' to it you first specify a lineStyle or fillStyle (or both), and then draw a shape. For example:
graphics.beginFill(0xff0000); graphics.drawCircle(50, 50, 100); graphics.endFill();
This will draw a circle shape to the Graphics object, with a diameter of 100, located at x: 50, y: 50.
When a Graphics object is rendered it will render differently based on if the game is running under Canvas or
WebGL. Under Canvas it will use the HTML Canvas context drawing operations to draw the path. Under WebGL the
graphics data is decomposed into polygons. Both of these are expensive processes, especially with complex shapes.
If your Graphics object doesn't change much (or at all) once you've drawn your shape to it, then you will help
performance by calling Graphics.generateTexture
. This will 'bake' the Graphics object into a Texture, and return it.
You can then use this Texture for Sprites or other display objects. If your Graphics object updates frequently then
you should avoid doing this, as it will constantly generate new textures, which will consume memory.
As you can tell, Graphics objects are a bit of a trade-off. While they are extremely useful, you need to be careful
in their complexity and quantity of them in your game.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
game | Phaser.Game | Current game instance. | ||
x | number | <optional> | 0 | X position of the new graphics object. |
y | number | <optional> | 0 | Y position of the new graphics object. |
A useful flag to control if the Game Object is alive or dead.
This is set automatically by the Health components damage
method should the object run out of health.
Or you can toggle it via your game code.
This property is mostly just provided to be used by your game - it doesn't effect rendering or logic updates.
However you can use Group.getFirstAlive
in conjunction with this property for fast object pooling and recycling.
The angle property is the rotation of the Game Object in degrees from its original orientation.
Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
Values outside this range are added to or subtracted from 360 to obtain a value within the range.
For example, the statement player.angle = 450 is the same as player.angle = 90.
If you wish to work in radians instead of degrees you can use the property rotation
instead.
Working in radians is slightly faster as it doesn't have to perform any calculations.
If the Game Object is enabled for animation (such as a Phaser.Sprite) this is a reference to its AnimationManager instance.
Through it you can create, play, pause and stop animations.
A Game Object with autoCull
set to true will check its bounds against the World Camera every frame.
If it is not intersecting the Camera bounds at any point then it has its renderable
property set to false
.
This keeps the Game Object alive and still processing updates, but forces it to skip the render step entirely.
This is a relatively expensive operation, especially if enabled on hundreds of Game Objects. So enable it only if you know it's required,
or you have tested performance and find it acceptable.
The blend mode to be applied to the graphic shape. Apply a value of PIXI.blendModes.NORMAL to reset the blend mode.
body
is the Game Objects physics body. Once a Game Object is enabled for physics you access all associated
properties and methods via it.
By default Game Objects won't add themselves to any physics system and their body
property will be null
.
To enable this Game Object for physics you need to call game.physics.enable(object, system)
where object
is this object
and system
is the Physics system you are using. If none is given it defaults to Phaser.Physics.Arcade
.
You can alternatively call game.physics.arcade.enable(object)
, or add this Game Object to a physics enabled Group.
Important: Enabling a Game Object for P2 or Ninja physics will automatically set its anchor
property to 0.5,
so the physics body is centered on the Game Object.
If you need a different result then adjust or re-create the Body shape offsets manually or reset the anchor after enabling physics.
The sum of the y and height properties.
This is the same as y + height - offsetY
.
The bounds' padding used for bounds calculation.
The x/y coordinate offset applied to the top-left of the camera that this Game Object will be drawn at if fixedToCamera
is true.
The values are relative to the top-left of the camera view and in addition to any parent of the Game Object on the display list.
The center x coordinate of the Game Object.
This is the same as (x - offsetX) + (width / 2)
.
The center y coordinate of the Game Object.
This is the same as (y - offsetY) + (height / 2)
.
If this is set to true
the Game Object checks if it is within the World bounds each frame.
When it is no longer intersecting the world bounds it dispatches the onOutOfBounds
event.
If it was previously out of bounds but is now intersecting the world bounds again it dispatches the onEnterBounds
event.
It also optionally kills the Game Object if outOfBoundsKill
is true
.
When checkWorldBounds
is enabled it forces the Game Object to calculate its full bounds every frame.
This is a relatively expensive operation, especially if enabled on hundreds of Game Objects. So enable it only if you know it's required,
or you have tested performance and find it acceptable.
[read-only] The array of children of this container.
The components this Game Object has installed.
An empty Object that belongs to this Game Object.
This value isn't ever used internally by Phaser, but may be used by your own code, or
by Phaser Plugins, to store data that needs to be associated with the Game Object,
without polluting the Game Object directly.
A debug flag designed for use with Game.enableStep
.
As a Game Object runs through its destroy method this flag is set to true,
and can be checked in any sub-systems or plugins it is being destroyed from.
All Phaser Game Objects have an Events class which contains all of the events that are dispatched when certain things happen to this
Game Object, or any of its components.
Controls if this Game Object is processed by the core game loop.
If this Game Object has a physics body it also controls if its physics body is updated or not.
When exists
is set to false
it will remove its physics body from the physics world if it has one.
It also toggles the visible
property to false as well.
Setting exists
to true will add its physics body back in to the physics world, if it has one.
It will also set the visible
property to true
.
The alpha value used when filling the Graphics object.
A Game Object that is "fixed" to the camera uses its x/y coordinates as offsets from the top left of the camera during rendering.
The values are adjusted at the rendering stage, overriding the Game Objects actual world position.
The end result is that the Game Object will appear to be 'fixed' to the camera, regardless of where in the game world
the camera is viewing. This is useful if for example this Game Object is a UI item that you wish to be visible at all times
regardless where in the world the camera is.
The offsets are stored in the cameraOffset
property.
Note that the cameraOffset
values are in addition to any parent of this Game Object on the display list.
Be careful not to set fixedToCamera
on Game Objects which are in Groups that already have fixedToCamera
enabled on them.
A Game Object is considered fresh
if it has just been created or reset and is yet to receive a renderer transform update.
This property is mostly used internally by the physics systems, but is exposed for the use of plugins.
A reference to the currently running Game.
The height of the displayObjectContainer, setting this will actually modify the scale to achieve the value set
If ignoreChildInput
is false
it will allow this objects children to be considered as valid for Input events.
If this property is true
then the children will not be considered as valid for Input events.
Note that this property isn't recursive: only immediate children are influenced, it doesn't scan further down.
Checks if the Game Objects bounds intersect with the Game Camera bounds.
Returns true
if they do, otherwise false
if fully outside of the Cameras bounds.
The Input Handler for this Game Object.
By default it is disabled. If you wish this Game Object to process input events you should enable it with: inputEnabled = true
.
After you have done this, this property will be a reference to the Phaser InputHandler.
By default a Game Object won't process any input events. By setting inputEnabled
to true a Phaser.InputHandler is created
for this Game Object and it will then start to process click / touch events and more.
You can then access the Input Handler via this.input
.
Note that Input related events are dispatched from this.events
, i.e.: events.onInputDown
.
If you set this property to false it will stop the Input Handler from processing any more input events.
If you want to temporarily disable input for a Game Object, then it's better to setinput.enabled = false
, as it won't reset any of the Input Handlers internal properties.
You can then toggle this back on as needed.
Checks if the Game Objects bounds are within, or intersect at any point with the Game World bounds.
Whether this shape is being used as a mask.
The key of the image or texture used by this Game Object during rendering.
If it is a string it's the string used to retrieve the texture from the Phaser Image Cache.
It can also be an instance of a RenderTexture, BitmapData, Video or PIXI.Texture.
If a Game Object is created without a key it is automatically assigned the key __default
which is a 32x32 transparent PNG stored within the Cache.
If a Game Object is given a key which doesn't exist in the Image Cache it is re-assigned the key __missing
which is a 32x32 PNG of a green box with a line through it.
The left coordinate of the Game Object.
This is the same as x - offsetX
.
The lifespan allows you to give a Game Object a lifespan in milliseconds.
Once the Game Object is 'born' you can set this to a positive value.
It is automatically decremented by the millisecond equivalent of game.time.physicsElapsed
each frame.
When it reaches zero it will call the kill
method.
Very handy for particles, bullets, collectibles, or any other short-lived entity.
The color of any lines drawn.
The width (thickness) of any lines drawn.
A user defined name given to this Game Object.
This value isn't ever used internally by Phaser, it is meant as a game level property.
The amount the Game Object is visually offset from its x coordinate.
This is the same as width * anchor.x
.
It will only be > 0 if anchor.x is not equal to zero.
The amount the Game Object is visually offset from its y coordinate.
This is the same as height * anchor.y
.
It will only be > 0 if anchor.y is not equal to zero.
If this and the checkWorldBounds
property are both set to true
then the kill
method is called as soon as inWorld
returns false.
If this and the autoCull
property are both set to true
, then the kill
method
is called as soon as the Game Object leaves the camera bounds.
A Game Object is that is pendingDestroy is flagged to have its destroy method called on the next logic update.
You can set it directly to allow you to flag an object to be destroyed on its next update.
This is extremely useful if you wish to destroy an object from within one of its own callbacks
such as with Buttons or other Input events.
The const physics body type of this object.
The position the Game Object was located in the previous frame.
The rotation the Game Object was in set to in the previous frame. Value is in radians.
The render order ID is used internally by the renderer and Input Manager and should not be modified.
This property is mostly used internally by the renderers, but is exposed for the use of plugins.
The right coordinate of the Game Object.
This is the same as x + width - offsetX
.
The tint applied to the graphic shape. This is a hex value. Apply a value of 0xFFFFFF to reset the tint.
The y coordinate of the Game Object.
This is the same as y - offsetY
.
The const type of this object.
The width of the displayObjectContainer, setting this will actually modify the scale to achieve the value set
The world coordinates of this Game Object in pixels.
Depending on where in the display list this Game Object is placed this value can differ from position
,
which contains the x/y coordinates relative to the Game Objects parent.
The position of the Game Object on the x axis relative to the local coordinates of the parent.
The position of the Game Object on the y axis relative to the local coordinates of the parent.
The z depth of this Game Object within its parent Group.
No two objects in a Group can have the same z value.
This value is adjusted automatically whenever the Group hierarchy changes.
If you wish to re-order the layering of a Game Object then see methods like Group.moveUp or Group.bringToTop.
Adds a child to the container.
Name | Type | Description |
---|---|---|
child | DisplayObject | The DisplayObject to add to the container |
The child that was added.
Adds a child to the container at a specified index. If the index is out of bounds an error will be thrown
Name | Type | Description |
---|---|---|
child | DisplayObject | The child to add |
index | Number | The index to place the child in |
The child that was added.
Aligns this Game Object within another Game Object, or Rectangle, known as the
'container', to one of 9 possible positions.
The container must be a Game Object, or Phaser.Rectangle object. This can include properties
such as World.bounds
or Camera.view
, for aligning Game Objects within the world
and camera bounds. Or it can include other Sprites, Images, Text objects, BitmapText,
TileSprites or Buttons.
Please note that aligning a Sprite to another Game Object does not make it a child of
the container. It simply modifies its position coordinates so it aligns with it.
The position constants you can use are:
Phaser.TOP_LEFT
, Phaser.TOP_CENTER
, Phaser.TOP_RIGHT
, Phaser.LEFT_CENTER
,Phaser.CENTER
, Phaser.RIGHT_CENTER
, Phaser.BOTTOM_LEFT
,Phaser.BOTTOM_CENTER
and Phaser.BOTTOM_RIGHT
.
The Game Objects are placed in such a way that their bounds align with the
container, taking into consideration rotation, scale and the anchor property.
This allows you to neatly align Game Objects, irrespective of their position value.
The optional offsetX
and offsetY
arguments allow you to apply extra spacing to the final
aligned position of the Game Object. For example:
sprite.alignIn(background, Phaser.BOTTOM_RIGHT, -20, -20)
Would align the sprite
to the bottom-right, but moved 20 pixels in from the corner.
Think of the offsets as applying an adjustment to the containers bounds before the alignment takes place.
So providing a negative offset will 'shrink' the container bounds by that amount, and providing a positive
one expands it.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
container | Phaser.Rectangle | Phaser.Sprite | Phaser.Image | Phaser.Text | Phaser.BitmapText | Phaser.Button | Phaser.Graphics | Phaser.TileSprite | The Game Object or Rectangle with which to align this Game Object to. Can also include properties such as | ||
position | integer | <optional> | The position constant. One of | |
offsetX | integer | <optional> | 0 | A horizontal adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it. |
offsetY | integer | <optional> | 0 | A vertical adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it. |
This Game Object.
Aligns this Game Object to the side of another Game Object, or Rectangle, known as the
'parent', in one of 11 possible positions.
The parent must be a Game Object, or Phaser.Rectangle object. This can include properties
such as World.bounds
or Camera.view
, for aligning Game Objects within the world
and camera bounds. Or it can include other Sprites, Images, Text objects, BitmapText,
TileSprites or Buttons.
Please note that aligning a Sprite to another Game Object does not make it a child of
the parent. It simply modifies its position coordinates so it aligns with it.
The position constants you can use are:
Phaser.TOP_LEFT
(default), Phaser.TOP_CENTER
, Phaser.TOP_RIGHT
, Phaser.LEFT_TOP
,Phaser.LEFT_CENTER
, Phaser.LEFT_BOTTOM
, Phaser.RIGHT_TOP
, Phaser.RIGHT_CENTER
,Phaser.RIGHT_BOTTOM
, Phaser.BOTTOM_LEFT
, Phaser.BOTTOM_CENTER
and Phaser.BOTTOM_RIGHT
.
The Game Objects are placed in such a way that their bounds align with the
parent, taking into consideration rotation, scale and the anchor property.
This allows you to neatly align Game Objects, irrespective of their position value.
The optional offsetX
and offsetY
arguments allow you to apply extra spacing to the final
aligned position of the Game Object. For example:
sprite.alignTo(background, Phaser.BOTTOM_RIGHT, -20, -20)
Would align the sprite
to the bottom-right, but moved 20 pixels in from the corner.
Think of the offsets as applying an adjustment to the parents bounds before the alignment takes place.
So providing a negative offset will 'shrink' the parent bounds by that amount, and providing a positive
one expands it.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
parent | Phaser.Rectangle | Phaser.Sprite | Phaser.Image | Phaser.Text | Phaser.BitmapText | Phaser.Button | Phaser.Graphics | Phaser.TileSprite | The Game Object or Rectangle with which to align this Game Object to. Can also include properties such as | ||
position | integer | <optional> | The position constant. One of | |
offsetX | integer | <optional> | 0 | A horizontal adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it. |
offsetY | integer | <optional> | 0 | A vertical adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it. |
This Game Object.
The arc method creates an arc/curve (used to create circles, or parts of circles).
Name | Type | Description |
---|---|---|
cx | Number | The x-coordinate of the center of the circle |
cy | Number | The y-coordinate of the center of the circle |
radius | Number | The radius of the circle |
startAngle | Number | The starting angle, in radians (0 is at the 3 o'clock position of the arc's circle) |
endAngle | Number | The ending angle, in radians |
anticlockwise | Boolean | Optional. Specifies whether the drawing should be counterclockwise or clockwise. False is default, and indicates clockwise, while true indicates counter-clockwise. |
segments | Number | Optional. The number of segments to use when calculating the arc. The default is 40. If you need more fidelity use a higher number. |
Specifies a simple one-color fill that subsequent calls to other Graphics methods
(such as lineTo() or drawCircle()) use when drawing.
Name | Type | Description |
---|---|---|
color | Number | the color of the fill |
alpha | Number | the alpha of the fill |
Calculate the points for a bezier curve and then draws it.
Name | Type | Description |
---|---|---|
cpX | Number | Control point x |
cpY | Number | Control point y |
cpX2 | Number | Second Control point x |
cpY2 | Number | Second Control point y |
toX | Number | Destination point x |
toY | Number | Destination point y |
Clears the graphics that were drawn to this Graphics object, and resets fill and line style settings.
Determines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself.
Name | Type | Description |
---|---|---|
child | DisplayObject | - |
Destroy this Graphics instance.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
destroyChildren | boolean | <optional> | true | Should every child of this object have its destroy method called? |
Destroys a previous cached sprite.
Draws a circle.
Name | Type | Description |
---|---|---|
x | Number | The X coordinate of the center of the circle |
y | Number | The Y coordinate of the center of the circle |
diameter | Number | The diameter of the circle |
Draws an ellipse.
Name | Type | Description |
---|---|---|
x | Number | The X coordinate of the center of the ellipse |
y | Number | The Y coordinate of the center of the ellipse |
width | Number | The half width of the ellipse |
height | Number | The half height of the ellipse |
Draws a polygon using the given path.
Name | Type | Description |
---|---|---|
path | Array | PhaserPolygon | The path data used to construct the polygon. Can either be an array of points or a Phaser.Polygon object. |
Name | Type | Description |
---|---|---|
x | Number | The X coord of the top-left of the rectangle |
y | Number | The Y coord of the top-left of the rectangle |
width | Number | The width of the rectangle |
height | Number | The height of the rectangle |
Name | Type | Description |
---|---|---|
x | Number | The X coord of the top-left of the rectangle |
y | Number | The Y coord of the top-left of the rectangle |
width | Number | The width of the rectangle |
height | Number | The height of the rectangle |
radius | Number | Radius of the rectangle corners. In WebGL this must be a value between 0 and 9. |
Draws the given shape to this Graphics object. Can be any of Circle, Rectangle, Ellipse, Line or Polygon.
Name | Type | Description |
---|---|---|
shape | Circle | Rectangle | Ellipse | Line | Polygon | The Shape object to draw. |
The generated GraphicsData object.
Applies a fill to the lines and shapes that were added since the last call to the beginFill() method.
Useful function that returns a texture of the graphics object that can then be used to create sprites
This can be quite useful if your geometry is complicated and needs to be reused multiple times.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
resolution | Number | <optional> | 1 | The resolution of the texture being generated |
scaleMode | Number | <optional> | 0 | Should be one of the PIXI.scaleMode consts |
padding | Number | <optional> | 0 | Add optional extra padding to the generated texture (default 0) |
a texture of the graphics object
Retrieves the bounds of the graphic shape as a rectangle object
the rectangular bounding area
Returns the child at the specified index
Name | Type | Description |
---|---|---|
index | Number | The index to get the child from |
The child at the given index, if any.
Returns the index position of a child DisplayObject instance
Name | Type | Description |
---|---|---|
child | DisplayObject | The DisplayObject instance to identify |
The index position of the child display object to identify
Retrieves the non-global local bounds of the graphic shape as a rectangle. The calculation takes all visible children into consideration.
The rectangular bounding area
Kills a Game Object. A killed Game Object has its alive
, exists
and visible
properties all set to false.
It will dispatch the onKilled
event. You can listen to events.onKilled
for the signal.
Note that killing a Game Object is a way for you to quickly recycle it in an object pool,
it doesn't destroy the object or free it up from memory.
If you don't need this Game Object any more you should call destroy
instead.
This instance.
Specifies the line style used for subsequent calls to Graphics methods such as the lineTo() method or the drawCircle() method.
Name | Type | Description |
---|---|---|
lineWidth | Number | width of the line to draw, will update the objects stored style |
color | Number | color of the line to draw, will update the objects stored style |
alpha | Number | alpha of the line to draw, will update the objects stored style |
Draws a line using the current line style from the current drawing position to (x, y);
The current drawing position is then set to (x, y).
Name | Type | Description |
---|---|---|
x | Number | the X coordinate to draw to |
y | Number | the Y coordinate to draw to |
Moves the current drawing position to x, y.
Name | Type | Description |
---|---|---|
x | Number | the X coordinate to move to |
y | Number | the Y coordinate to move to |
Automatically called by World
Automatically called by World.preUpdate.
Calculate the points for a quadratic bezier curve and then draws it.
Based on: https://stackoverflow.com/questions/785097/how-do-i-implement-a-bezier-curve-in-c
Name | Type | Description |
---|---|---|
cpX | Number | Control point x |
cpY | Number | Control point y |
toX | Number | Destination point x |
toY | Number | Destination point y |
Removes a child from the container.
Name | Type | Description |
---|---|---|
child | DisplayObject | The DisplayObject to remove |
The child that was removed.
Removes a child from the specified index position.
Name | Type | Description |
---|---|---|
index | Number | The index to get the child from |
The child that was removed.
Removes all children from this container that are within the begin and end indexes.
Name | Type | Description |
---|---|---|
beginIndex | Number | The beginning position. Default value is 0. |
endIndex | Number | The ending position. Default value is size of the container. |
Resets the Game Object.
This moves the Game Object to the given x/y world coordinates and sets fresh
, exists
,visible
and renderable
to true.
If this Game Object has the LifeSpan component it will also set alive
to true and health
to the given value.
If this Game Object has a Physics Body it will reset the Body.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
x | number | The x coordinate (in world space) to position the Game Object at. | ||
y | number | The y coordinate (in world space) to position the Game Object at. | ||
health | number | <optional> | 1 | The health to give the Game Object if it has the Health component. |
This instance.
Brings a 'dead' Game Object back to life, optionally resetting its health value in the process.
A resurrected Game Object has its alive
, exists
and visible
properties all set to true.
It will dispatch the onRevived
event. Listen to events.onRevived
for the signal.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
health | number | <optional> | 100 | The health to give the Game Object. Only set if the GameObject has the Health component. |
This instance.
Changes the position of an existing child in the display object container
Name | Type | Description |
---|---|---|
child | DisplayObject | The child DisplayObject instance for which you want to change the index number |
index | Number | The resulting index number for the child display object |
Swaps the position of 2 Display Objects within this container.
Name | Type | Description |
---|---|---|
child | DisplayObject | - |
child2 | DisplayObject | - |
Override this method in your own custom objects to handle any update requirements.
It is called immediately after preUpdate
and before postUpdate
.
Remember if this Game Object has any children you should call update on those too.
Update the bounds of the object
© 2016 Richard Davey, Photon Storm Ltd.
Licensed under the MIT License.
http://phaser.io/docs/2.6.2/Phaser.Graphics.html