Touch controls

Taps, drags, holds, swipes, and several fingers at once: how a player controls your game on a phone.

Try each example, then copy the words under it and paste them to your AI. You can also send your AI the link to an example.

Tapping

The simplest control there is, and the one every phone game starts with.

Tap anywhere

Something happens where the finger lands.

Tap anywhere in the example.

Ask your AI

When the player taps anywhere, make a star appear where they tapped.

View code
if (game.isPointerPressed()) {
  const item = game.add(new ImageSprite(game, "star", game.pointerX, game.pointerY));
  game.animateIntegrate(item);
}

Tap an object

Only a tap on the object counts, which is how buttons and targets work.

Tap the coin, then tap next to it.

Ask your AI

Only count a point when the player taps the coin itself; if they miss, shake the coin.

View code
if (game.isPointerPressedOverSprite(coin)) {
  score += 1;
  game.animatePulse(coin, 1.3, 200);
} else if (game.isPointerPressed()) {
  game.animateShake(coin, 12, 250);
}

Hold

Something happens for as long as the finger stays down.

Press and hold inside the example.

1400
Ask your AI

While the player holds a finger down, make the ball rise; when they let go, it falls.

View code
if (game.isPointerDown()) {
  ball.vy -= 1400 * dt;
}

Moving things

Controls where the finger moves, not only where it lands.

Drag

The object follows the finger that picked it up.

Drag the gem.

Ask your AI

Let the player drag the gem around with a finger.

View code
if (game.isPointerPressedOverSprite(gem)) dragging = true;
if (!game.isPointerDown()) dragging = false;
if (dragging) {
  gem.x = game.pointerX;
  gem.y = game.pointerY;
}

Follow the finger

The object glides toward the finger instead of jumping to it, which feels smoother for a paddle or a basket.

Hold a finger down and move it.

Ask your AI

Make the basket slide toward my finger smoothly, only left and right.

View code
if (game.isPointerDown()) {
  heart.x += (game.pointerX - heart.x) * Math.min(1, 8 * dt);
}

Swipe

A quick flick in one direction, as in lane runners and puzzle games.

Swipe left or right.

60px
Ask your AI

Move the ball one lane when the player swipes left or right.

View code
// On release, compare where the finger started and ended.
const dx = endX - startX;
if (Math.abs(dx) > 60) lane += Math.sign(dx);

Several fingers

Each finger is tracked on its own, so a player can steer with one thumb and fire with the other.

On a touchscreen, put several fingers on the example.

Ask your AI

Let the player hold the left side to steer and tap the right side to fire at the same time.

View code
for (const finger of game.getPointers()) {
  if (finger.x < game.width / 2) steer(finger.x);
  else fire();
}

Keyboard

Players on a computer have a keyboard, but players on a phone do not.

Arrow keys

Keys work on computers only, so a phone game needs a touch control for the same thing.

On a computer, click the example, then use the arrow keys.

400px/s
Ask your AI

Move the ball with the arrow keys on a computer, and by dragging on a phone.

View code
if (game.isKeyDown("ArrowLeft")) ball.x -= 400 * dt;
if (game.isKeyDown("ArrowRight")) ball.x += 400 * dt;