on Hit Wall

Run some code when a sprite contacts a wall.

Playerspritelocationonof kindhits wall at
JavaScript
function scene.onHitWall(kind: number, handler: (sprite: Sprite, location: tiles.Location) => void): void;
Python
def scene.on_hit_wall(kind: number, handler: (Sprite, tiles.Location) -> None): None

We can detect when a moving sprite contacts a wall in the scene. You can then run some code if your sprite touches a location that is set as a wall. You choose the sprite kind to check for wall collisions.

When a wall hit is detected the sprite of the kind you asked for is given to you in the sprite parameter of handler.

A a sprite hitting a wall is dectected when the outside edges of their images make contact. If a sprite has it’s ghost flag set, any contact with the wall is ignored.

Parameters

  • kind: the type of sprite to check for a wall hit.
  • handler: the code to run when the sprite makes contact.
    • sprite: the sprite that hit the wall tile.

Example

Build a brick wall of tiles in the scene. Use the A to send a Ghost sprite toward the wall and watch it stop. When button B is pressed, switch the value of the ghost flag and see if the ghost sprite hits the wall or goes through.

ghostOFFghostscreen width1-ghost-100setvx (velocity x)tosetrighttosetghostonAbuttonpressedghostONghostscreen width1-ghost-100setvx (velocity x)tosetrighttosetghostonBbuttonpressedSpriteKind.Ghostspriteof kindghostscreen width1-scene.onHitTile(SpriteKind.Ghost, 2, function (sprite) {    sprite.say("Ouch!", 200)})setrighttosetghosttoscene.setTile(2, img`1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 f 2 1 1 2 2 f 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 f 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 f 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 f 2 2 2 f 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 f 2 2 2 2 2 2 2 2 f 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 f 2 2 2 2 f 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 `, true)enum SpriteKind {    Ghost,    Ball}on start

See also

is hitting wall, tile hit of kind, overlaps with, set flag