Skip to main content
Version: Phaser v4.0.0

Grid

The Grid Shape is a Game Object that can be added to a Scene, Group or Container. You can treat it like any other Game Object in your game, such as tweening it, scaling it, or enabling it for input or physics. It provides a quick and easy way for you to render this shape in your game without using a texture, while still taking advantage of being fully batched in WebGL.

A Grid Shape allows you to display a grid in your game, where you can control the size of the grid as well as the width and height of the grid cells. You can set a fill color for each grid cell as well as an alternate fill color. When the alternate fill color is set then the grid cells will alternate the fill colors as they render, creating a chess-board effect. You can also optionally have a stroke fill color. If set, this draws lines between the grid cells in the given color. If you specify a stroke color with an alpha of zero, then it will draw the cells spaced out, but without the lines between them.

Constructor

new Grid(scene, [x], [y], [width], [height], [cellWidth], [cellHeight], [fillColor], [fillAlpha], [strokeFillColor], [strokeFillAlpha])

Parameters

nametypeoptionaldefaultdescription
scenePhaser.SceneNoThe Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
xnumberYes0The horizontal position of this Game Object in the world.
ynumberYes0The vertical position of this Game Object in the world.
widthnumberYes128The width of the grid.
heightnumberYes128The height of the grid.
cellWidthnumberYes32The width of one cell in the grid.
cellHeightnumberYes32The height of one cell in the grid.
fillColornumberYesThe color the grid cells will be filled with, i.e. 0xff0000 for red.
fillAlphanumberYesThe alpha the grid cells will be filled with. You can also set the alpha of the overall Shape using its alpha property.
strokeFillColornumberYesThe color of the lines between the grid cells. See the setStrokeStyle method.
strokeFillAlphanumberYesThe alpha of the lines between the grid cells.

Scope: static

Extends

Phaser.GameObjects.Shape

Source: src/gameobjects/shape/grid/Grid.js#L11
Since: 3.13.0

Inherited Members

From Phaser.GameObjects.Components.AlphaSingle:

From Phaser.GameObjects.Components.BlendMode:

From Phaser.GameObjects.Components.Depth:

From Phaser.GameObjects.Components.Filters:

From Phaser.GameObjects.Components.Mask:

From Phaser.GameObjects.Components.Origin:

From Phaser.GameObjects.Components.RenderNodes:

From Phaser.GameObjects.Components.ScrollFactor:

From Phaser.GameObjects.Components.Transform:

From Phaser.GameObjects.Components.Visible:

From Phaser.GameObjects.GameObject:

From Phaser.GameObjects.Shape:


Public Members

altFillAlpha

altFillAlpha: number

Description:

The alpha the alternating grid cells will be filled with. You can also set the alpha of the overall Shape using its alpha property.

Source: src/gameobjects/shape/grid/Grid.js#L104
Since: 3.13.0


altFillColor

altFillColor: number

Description:

The color the alternating grid cells will be filled with, i.e. 0xff0000 for red.

Source: src/gameobjects/shape/grid/Grid.js#L95
Since: 3.13.0


cellHeight

cellHeight: number

Description:

The height of each grid cell. Must be a positive value.

Source: src/gameobjects/shape/grid/Grid.js#L75
Since: 3.13.0


cellPadding

cellPadding: number

Description:

The padding around each cell. The effective gutter between cells is twice this value.

Source: src/gameobjects/shape/grid/Grid.js#L114
Since: 4.0.0


cellWidth

cellWidth: number

Description:

The width of each grid cell. Must be a positive value.

Source: src/gameobjects/shape/grid/Grid.js#L65
Since: 3.13.0


showAltCells

showAltCells: boolean

Description:

Controls whether the grid renders alternating cells using the altFillColor and altFillAlpha values. Set this via the setAltFillStyle method.

Source: src/gameobjects/shape/grid/Grid.js#L85
Since: 3.13.0


strokeOutside

strokeOutside: boolean

Description:

Whether to stroke on the outside edges of the Grid object.

Source: src/gameobjects/shape/grid/Grid.js#L125
Since: 4.0.0


strokeOutsideIncomplete

strokeOutsideIncomplete: boolean

Description:

Whether to stroke on the outside edges of the Grid object when the cell is incomplete, e.g. the grid size does not evenly fit the cell size.

This only has an effect if strokeOutside is true. It will affect the right and bottom edges of the grid.

Source: src/gameobjects/shape/grid/Grid.js#L135
Since: 4.0.0


Inherited Methods

From Phaser.Events.EventEmitter:

From Phaser.GameObjects.Components.AlphaSingle:

From Phaser.GameObjects.Components.BlendMode:

From Phaser.GameObjects.Components.Depth:

From Phaser.GameObjects.Components.Filters:

From Phaser.GameObjects.Components.GetBounds:

From Phaser.GameObjects.Components.Mask:

From Phaser.GameObjects.Components.Origin:

From Phaser.GameObjects.Components.RenderNodes:

From Phaser.GameObjects.Components.RenderSteps:

From Phaser.GameObjects.Components.ScrollFactor:

From Phaser.GameObjects.Components.Transform:

From Phaser.GameObjects.Components.Visible:

From Phaser.GameObjects.GameObject:

From Phaser.GameObjects.Shape:


Public Methods

setAltFillStyle

<instance> setAltFillStyle([fillColor], [fillAlpha])

Description:

Sets the fill color and alpha level that the alternating grid cells will use.

If this method is called with no values then alternating grid cells will not be rendered in a different color.

Also see the setStrokeStyle and setFillStyle methods.

This call can be chained.

Parameters:

nametypeoptionaldefaultdescription
fillColornumberYesThe color the alternating grid cells will be filled with, i.e. 0xff0000 for red.
fillAlphanumberYes1The alpha the alternating grid cells will be filled with. You can also set the alpha of the overall Shape using its alpha property.

Returns: Phaser.GameObjects.Grid - This Game Object instance.

Source: src/gameobjects/shape/grid/Grid.js#L163
Since: 3.13.0


setCellPadding

<instance> setCellPadding([value])

Description:

Sets the cell padding for the grid. The cell padding is the space around each cell, between the cells. The effective gutter between cells is twice this value.

If this method is called with no value then the cell padding is set to zero.

This call can be chained.

Parameters:

nametypeoptionaldescription
valuenumberYesThe cell padding value.

Returns: Phaser.GameObjects.Grid - This Game Object instance.

Source: src/gameobjects/shape/grid/Grid.js#L198
Since: 4.0.0


setStrokeOutside

<instance> setStrokeOutside(strokeOutside, [strokeOutsideIncomplete])

Description:

Controls whether a stroke is drawn around the outer perimeter of the entire Grid object, in addition to the lines drawn between cells. Optionally, you can also control whether the outer edge is stroked on partial cells, i.e. where the grid dimensions do not divide evenly by the cell dimensions, leaving an incomplete cell on the right or bottom edge.

This call can be chained.

Parameters:

nametypeoptionaldescription
strokeOutsidebooleanNoWhether to stroke the outside edges of the Grid object.
strokeOutsideIncompletebooleanYesWhether to stroke the outside edges of the Grid object when the cell is incomplete.

Source: src/gameobjects/shape/grid/Grid.js#L219
Since: 4.0.0