Tilemap
class Tilemap extends PIXI.Container
A rectangular tilemap implementation that renders a predefined set of tile textures.
The tileset of a tilemap defines the list of base-textures that can be painted in the tilemap. A texture is identified using its base-texture's index into the this list, i.e. changing the base-texture at a given index in the tileset modifies the paint of all tiles pointing to that index.
The size of the tileset is limited by the texture units supported by the client device. The minimum supported
value is 8, as defined by the WebGL 1 specification. gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS
) can be used
to extract this limit. CompositeTilemap can be used to get around this limit by layering multiple tilemap
instances.
import { Tilemap } from '@pixi/tilemap';
import { Loader } from '@pixi/loaders';
// Add the spritesheet into your loader!
Loader.shared.add('atlas', 'assets/atlas.json');
// Make the tilemap once the tileset assets are available.
Loader.shared.load(function onTilesetLoaded()
{
// The base-texture is shared between all the tile textures.
const tilemap = new Tilemap([Texture.from('grass.png').baseTexture])
.tile('grass.png', 0, 0)
.tile('grass.png', 100, 100)
.tile('brick_wall.png', 0, 100);
});
Constructor
new Tilemap(tileset: PIXI.BaseTexture | PIXI.BaseTexture<Array>) → {}
Name | Type | Description |
---|---|---|
tileset | PIXI.BaseTexture | PIXI.BaseTexture<Array> |
The tileset to use for the tilemap. This can be reset later with Tilemap.setTileset. The base-textures in this array must not be duplicated. |
Summary
Properties from Tilemap
[number, number] |
The tile animation frame. |
boolean |
Flags whether any animated tile was added. |
The local bounds of the tilemap itself. This does not include DisplayObject children. |
|
Array<PIXI.BaseTexture> |
The list of base-textures being used in the tilemap. |
Methods from Tilemap
boolean |
Deprecated signature for tile. |
this |
Deprecated signature for tile. |
this |
Clears all the tiles added into this tilemap. |
void |
|
PIXI.Rectangle |
|
Array<PIXI.BaseTexture> |
|
void |
|
this |
Define the tileset used by the tilemap. |
this |
Adds a tile that paints the given texture at (x, y). |
void |
Changes the |
void |
Changes the |
void |
Changes the |
void |
Changes the rotation of the last tile. |
void |
|
Properties inherited from Container
T[] |
|
number |
The height of the Container, setting this will actually modify the scale to achieve the value set. |
boolean |
|
boolean |
|
boolean |
Should children be sorted by zIndex at the next updateTransform call. |
number |
The width of the Container, setting this will actually modify the scale to achieve the value set. |
Properties inherited from DisplayObject
Methods inherited from Container
Methods inherited from DisplayObject
Inherited Events from Container
Inherited Events from DisplayObject
|
|
|
|
|
|
Capture phase equivalent of |
|
|
|
|
|
Capture phase equivalent of |
|
Fired when the mouse pointer is moved over a DisplayObject and its descendant's hit testing boundaries. |
|
Capture phase equivalent of |
|
Fired when the mouse pointer exits a DisplayObject and its descendants. |
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
Fired when the pointer is moved over a DisplayObject and its descendant's hit testing boundaries. |
|
Capture phase equivalent of |
|
Fired when the pointer leaves the hit testing boundaries of a DisplayObject and its descendants. |
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
PIXI.FederatedWheelEvent |
Fired when a the user scrolls with the mouse cursor over a DisplayObject. |
PIXI.FederatedWheelEvent |
Capture phase equivalent of |
Public Properties
tileAnim: [number, number] = undefined
The tile animation frame.
Protected Properties
protected hasAnimatedTile: boolean = false
Flags whether any animated tile was added.
protected tilemapBounds
The local bounds of the tilemap itself. This does not include DisplayObject children.
protected tileset: Array<PIXI.BaseTexture>
The list of base-textures being used in the tilemap.
This should not be shuffled after tiles have been added into this tilemap. Usually, only tile textures should be added after tiles have been added into the map.
Public Methods
addFrame(texture: PIXI.Texture | string | number, x: number, y: number, animX: number, animY: number) → {boolean}
Deprecated signature for tile.
Name | Type | Description |
---|---|---|
texture | PIXI.Texture | string | number | |
x | number | |
y | number | |
animX | number | |
animY | number |
Type | Description |
---|---|
boolean |
addRect(textureIndex: number, u: number, v: number, x: number, y: number, tileWidth: number, tileHeight: number, animX: number, animY: number, rotate: number, animCountX: number, animCountY: number, animDivisor: number, alpha: number) → {this}
Deprecated signature for tile.
Name | Type | Default | Description |
---|---|---|---|
textureIndex | number | ||
u | number | ||
v | number | ||
x | number | ||
y | number | ||
tileWidth | number | ||
tileHeight | number | ||
animX | number | 0 | |
animY | number | 0 | |
rotate | number | 0 | |
animCountX | number | 1024 | |
animCountY | number | 1024 | |
animDivisor | number | 1 | |
alpha | number | 1 |
Type | Description |
---|---|
this |
clear() → {this}
Clears all the tiles added into this tilemap.
Type | Description |
---|---|
this |
destroy(options: IDestroyOptions) → {void}
Name | Type | Attributes | Description |
---|---|---|---|
options | IDestroyOptions |
<optional> |
Type | Description |
---|---|
void |
getLocalBounds(rect: PIXI.Rectangle) → {PIXI.Rectangle}
Name | Type | Attributes | Description |
---|---|---|---|
rect | PIXI.Rectangle |
<optional> |
Type | Description |
---|---|
PIXI.Rectangle |
getTileset() → {Array<PIXI.BaseTexture>}
Type | Description |
---|---|
Array<PIXI.BaseTexture> |
The tileset of this tilemap. |
renderCanvas(renderer: PIXI.CanvasRenderer) → {void}
Renders the object using the Canvas renderer
Name | Type | Description |
---|---|---|
renderer | PIXI.CanvasRenderer |
The renderer |
Type | Description |
---|---|
void |
setTileset(tileset: PIXI.BaseTexture | PIXI.BaseTexture<Array>) → {this}
Define the tileset used by the tilemap.
Name | Type | Description |
---|---|---|
tileset | PIXI.BaseTexture | PIXI.BaseTexture<Array> |
The list of textures to use in the tilemap. If a base-texture (not array) is passed, it will be wrapped into an array. This should not contain any duplicates. |
Type | Description |
---|---|
this |
tile(tileTexture: number | string | PIXI.Texture | PIXI.BaseTexture, x: number, y: number, options: { u? : number, v? : number, tileWidth? : number, tileHeight? : number, animX? : number, animY? : number, rotate? : number, animCountX? : number, animCountY? : number, animDivisor? : number, alpha? : number }) → {this}
Adds a tile that paints the given texture at (x, y).
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
tileTexture | number | string | PIXI.Texture | PIXI.BaseTexture |
The tiling texture to render. |
||
x | number |
The local x-coordinate of the tile's position. |
||
y | number |
The local y-coordinate of the tile's position. |
||
options | { u? : number, v? : number, tileWidth? : number, tileHeight? : number, animX? : number, animY? : number, rotate? : number, animCountX? : number, animCountY? : number, animDivisor? : number, alpha? : number } |
Additional tile options. |
||
options.u |
<optional> |
texture.frame.x |
The x-coordinate of the texture in its base-texture's space. |
|
options.v |
<optional> |
texture.frame.y |
The y-coordinate of the texture in its base-texture's space. |
|
options.tileWidth |
<optional> |
texture.orig.width |
The local width of the tile. |
|
options.tileHeight |
<optional> |
texture.orig.height |
The local height of the tile. |
|
options.animX |
<optional> |
0 |
For animated tiles, this is the "offset" along the x-axis for adjacent animation frame textures in the base-texture. |
|
options.animY |
<optional> |
0 |
For animated tiles, this is the "offset" along the y-axis for adjacent animation frames textures in the base-texture. |
|
options.rotate |
<optional> |
0 | ||
options.animCountX |
<optional> |
1024 |
For animated tiles, this is the number of animation frame textures per row. |
|
options.animCountY |
<optional> |
1024 |
For animated tiles, this is the number of animation frame textures per column. |
|
options.animDivisor |
<optional> |
1 |
For animated tiles, this is the animation duration of each frame |
|
options.alpha |
<optional> |
1 |
Tile alpha |
Type | Description |
---|---|
this |
This tilemap, good for chaining. |
tileAnimDivisor(divisor: number) → {void}
Changes the animDivisor
value of the last tile.
Name | Type | Description |
---|---|---|
divisor | number |
Type | Description |
---|---|
void |
tileAnimX(offset: number, count: number) → {void}
Changes the animX
, animCountX
of the last tile.
Name | Type | Description |
---|---|---|
offset | number | |
count | number |
Type | Description |
---|---|
void |
tileAnimY(offset: number, count: number) → {void}
Changes the animY
, animCountY
of the last tile.
Name | Type | Description |
---|---|---|
offset | number | |
count | number |
Type | Description |
---|---|
void |
tileRotate(rotate: number) → {void}
Changes the rotation of the last tile.
Name | Type | Description |
---|---|---|
rotate | number |
Type | Description |
---|---|
void |
Protected Methods
protected _calculateBounds() → {void}
Type | Description |
---|---|
void |