Scenery with tiles
You want to build the level by painting the floor and the walls, without dragging an object for every block.
In the editor
Section titled “In the editor”- Tileset. Import the image of the tiles as a sprite, slice it to the tile size and create the tileset with + › Novo tileset… (new tileset). In its tab, click the tiles that are solid (floor, wall).
- Layer. In the Camadas panel, + Camada › Tiles. Name it
Chao. - Paint. Choose a tile in the Inspector’s palette and drag in the room. The right button erases.
The full step by step is in Tileset and tiles.
In the code
Section titled “In the code”Nothing changes. The platformer player is the same as when the floor was a solid object:
void Player::onUpdate() { hspeed = axis("esquerda", "direita") * 1.5f; if (pressed("pular") && onGround()) vspeed = -4.5f; moveAndStop();}How it works
Section titled “How it works”- The tiles marked as solid in the tileset block movement, like an object with Sólido (solid) checked.
moveAndStop()stops at the tile, andonGround()sees the tile under the player.- Collision is done through the grid, looking only at the tiles the player’s box touches. A big level, full of tiles, does not get slower.
Variations
Section titled “Variations”Decorations that do not collide. Create another Tiles layer, Enfeites, and turn Colide (collides) off in the Inspector. Paint the grass, the flowers and the signs on it.
Knowing which tile the player is on. tileAt returns the number of the tile at a point (or -1 if there is no tile), and solidAt tells whether there is something solid there:
if (tileAt("Chao", x, y + 12) == 7) Audio::play("passo_na_pedra");if (solidAt(x + 10, y)) flipX = !flipX;Edges and corners by themselves. Mark a terrain in the tileset and use the terrain brush. See Tileset and tiles.
See also
Section titled “See also”- The Plataforma template, in Novo projeto (new project), has a ready-made level built this way.
- Platformer.
- Layers and tiles.
