CompositeTilemap
class CompositeTilemap extends PIXI.Container
A tilemap composite that lazily builds tilesets layered into multiple tilemaps.
The composite tileset is the concatenatation of the individual tilesets used in the tilemaps. You can preinitialized it by passing a list of tile textures to the constructor. Otherwise, the composite tilemap is lazily built as you add more tiles with newer tile textures. A new tilemap is created once the last tilemap has reached its limit (as set by texturesPerTilemap).
import { Application } from '@pixi/app';
import { CompositeTilemap } from '@pixi/tilemap';
import { Loader } from '@pixi/loaders';
// Setup view & stage.
const app = new Application();
document.body.appendChild(app.renderer.view);
app.stage.interactive = true;
// Global reference to the tilemap.
let globalTilemap: CompositeTilemap;
// Load the tileset spritesheet!
Loader.shared.load('atlas.json');
// Initialize the tilemap scene when the assets load.
Loader.shared.load(function onTilesetLoaded()
{
const tilemap = new CompositeTilemap();
// Setup the game level with grass and dungeons!
for (let x = 0; x < 10; x++)
{
for (let y = 0; y < 10; y++)
{
tilemap.tile(
x % 2 === 0 && (x === y || x + y === 10) ? 'dungeon.png' : 'grass.png',
x * 100,
y * 100,
);
}
}
globalTilemap = app.stage.addChild(tilemap);
});
// Show a bomb at a random location whenever the user clicks!
app.stage.on('click', function onClick()
{
if (!globalTilemap) return;
const x = Math.floor(Math.random() * 10);
const y = Math.floor(Math.random() * 10);
globalTilemap.tile('bomb.png', x * 100, y * 100);
});
Constructor
new CompositeTilemap(tileset: Array<PIXI.BaseTexture>) → {}
Name | Type | Attributes | Description |
---|---|---|---|
tileset | Array<PIXI.BaseTexture> |
<optional> |
A list of tile base-textures that will be used to eagerly initialized the layered tilemaps. This is only an performance optimization, and using tile will work equivalently. |
Summary
Properties from CompositeTilemap
Alias for tileset. |
|
number |
|
number |
The hard limit on the number of tile textures used in each tilemap. |
[number, number] |
The animation frame vector. |
Tilemap |
The last modified tilemap. |
Methods from CompositeTilemap
this |
|
this |
|
this |
Clears the tilemap composite. |
void |
|
this |
Adds a tile that paints the given tile texture at (x, y). |
this |
Changes |
this |
Changes |
this |
Changes |
this |
Changes the rotation of the last added tile. |
this |
This will preinitialize the tilesets of the layered tilemaps. |
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
texturesPerTilemap: number
The hard limit on the number of tile textures used in each tilemap.
tileAnim: [number, number] = undefined
The animation frame vector.
Animated tiles have four parameters - animX
, animY
, animCountX
, animCountY
. The textures
of adjacent animation frames are at offset animX
or animY
of each other, with animCountX
per
row and animCountY
per column.
The animation frame vector specifies which animation frame texture to use. If the x/y coordinate is
larger than the animCountX
or animCountY
for a specific tile, the modulus is taken.
Protected Properties
protected lastModifiedTilemap: Tilemap = undefined
The last modified tilemap.
Public Methods
addFrame(texture: PIXI.Texture | string | number, x: number, y: number, animX: number, animY: number, animWidth: number, animHeight: number, animDivisor: number, alpha: number) → {this}
Name | Type | Attributes | Description |
---|---|---|---|
texture | PIXI.Texture | string | number | ||
x | number | ||
y | number | ||
animX | number |
<optional> |
|
animY | number |
<optional> |
|
animWidth | number |
<optional> |
|
animHeight | number |
<optional> |
|
animDivisor | number |
<optional> |
|
alpha | number |
<optional> |
Type | Description |
---|---|
this |
addRect(textureIndex: number, u: number, v: number, x: number, y: number, tileWidth: number, tileHeight: number, animX: number, animY: number, rotate: number, animWidth: number, animHeight: number) → {this}
Name | Type | Attributes | Description |
---|---|---|---|
textureIndex | number | ||
u | number | ||
v | number | ||
x | number | ||
y | number | ||
tileWidth | number | ||
tileHeight | number | ||
animX | number |
<optional> |
|
animY | number |
<optional> |
|
rotate | number |
<optional> |
|
animWidth | number |
<optional> |
|
animHeight | number |
<optional> |
Type | Description |
---|---|
this |
clear() → {this}
Clears the tilemap composite.
Type | Description |
---|---|
this |
renderCanvas(renderer: PIXI.CanvasRenderer) → {void}
Renders the object using the Canvas renderer
Name | Type | Description |
---|---|---|
renderer | PIXI.CanvasRenderer |
The renderer |
Type | Description |
---|---|
void |
tile(tileTexture: PIXI.Texture | string | number, 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 tile texture at (x, y).
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
tileTexture | PIXI.Texture | string | number |
The tile texture. You can pass an index into the composite tilemap as well. |
||
x | number |
The local x-coordinate of the tile's location. |
||
y | number |
The local y-coordinate of the tile's location. |
||
options | { u? : number, v? : number, tileWidth? : number, tileHeight? : number, animX? : number, animY? : number, rotate? : number, animCountX? : number, animCountY? : number, animDivisor? : number, alpha? : number } |
Additional options to pass to Tilemap.tile. |
||
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 each frame |
|
options.alpha |
<optional> |
1 |
Tile alpha |
Type | Description |
---|---|
this |
This tilemap, good for chaining. |
tileAnimDivisor(divisor: number) → {this}
Changes tileAnimDivisor
value of the last added tile.
Name | Type | Description |
---|---|---|
divisor | number |
Type | Description |
---|---|
this |
tileAnimX(offset: number, count: number) → {this}
Changes animX
, animCountX
of the last added tile.
Name | Type | Description |
---|---|---|
offset | number | |
count | number |
Type | Description |
---|---|
this |
tileAnimY(offset: number, count: number) → {this}
Changes animY
, animCountY
of the last added tile.
Name | Type | Description |
---|---|---|
offset | number | |
count | number |
Type | Description |
---|---|
this |
tileRotate(rotate: number) → {this}
Changes the rotation of the last added tile.
Name | Type | Description |
---|---|---|
rotate | number |
Type | Description |
---|---|
this |
tileset(tileTextures: Array<PIXI.BaseTexture>) → {this}
This will preinitialize the tilesets of the layered tilemaps.
If used after a tilemap has been created (or a tile added), this will overwrite the tile textures of the existing tilemaps. Passing the tileset to the constructor instead is the best practice.
Name | Type | Description |
---|---|---|
tileTextures | Array<PIXI.BaseTexture> |
The list of tile textures that make up the tileset. |
Type | Description |
---|---|
this |