GameSpark/Tutorials Open editor ↗
Add gravity · Step 3 of 3

Onto your tile map

A flat GROUND line is fine, but real platformers have ledges, gaps, and floors at different heights. In this recipe your character falls onto the tile map you painted yourself — landing on solid tiles and falling through empty space. The trick that makes it work is sprite flags.

You’ll want the Drawing sprites, Render tiles with map(), and Booleans lessons under your belt first.

1. Mark tiles as solid with a flag

Every sprite carries eight little on/off switches called flags. We’ll use flag 0 to mean “you can stand on this.” In the Sprites tab, click your ground/platform tile, find the row of flag toggles, and turn on flag 0. Do that for every tile you want to be solid; leave background tiles (sky, decoration) with it off.

In code, fget(n, 0) reads flag 0 of sprite n and gives back a boolean — true if you switched it on:

function draw() {
  cls(1);
  // Is sprite 1 marked solid? (flag 0)
  print('sprite 1 solid? ' + fget(1, 0), 8, 8, 7);
}

2. Ask the map what’s under the feet

mget(col, row) tells you which sprite sits in a map cell. The map grid is 8-pixel tiles, so the cell at a pixel (px, py) is flr(px / 8) across and flr(py / 8) down. Chain the two together and you can ask “is the tile at this pixel solid?”:

// Is the tile covering pixel (px, py) a solid one?
function solid(px, py) {
  let tile = mget(flr(px / 8), flr(py / 8));
  return fget(tile, 0);
}
💡

This is why flags beat a list of “solid tile numbers”: you decide what’s solid by clicking in the Sprites tab, and the code never changes. Repaint your level with new tiles and it still just works.

3. Land on the tile, not a fixed line

Now replace the single GROUND check with a check against the tile under the feet. When the feet (y + 8) are inside a solid tile while falling, snap the sprite so its feet rest on top of that tile — flr((y + 8) / 8) * 8 is the top edge of the tile row the feet are in:

dy = dy + GRAVITY;
y = y + dy;

// Check both bottom corners so you don't slip off edges.
let onGround = false;
if (dy >= 0 && (solid(x, y + 8) || solid(x + 7, y + 8))) {
  y = flr((y + 8) / 8) * 8 - 8;   // feet on top of the tile
  dy = 0;
  onGround = true;
}

if (keyp(' ') && onGround) {
  dy = JUMP;
}

Checking both bottom corners (x and x + 7) means the character stays up as long as either foot is over solid ground — so it can stand on the very edge of a ledge without sliding off. Send the full program to the editor, paint some platforms, and jump between them.

🎯

For hitting other sprites — a coin, an enemy — reach for collide(x1, y1, x2, y2) instead: it tests whether two 8×8 boxes overlap. It’s the right tool for sprite-vs-sprite, but not for standing on tiles, which is why floors use mget + fget.

What you learned