pxt-common-packages
Version:
Microsoft MakeCode (PXT) common packages
87 lines (74 loc) • 3.03 kB
Markdown
Check if a sprite is currently hitting a wall.
```sig
sprites.create(null).isHittingTile(CollisionDirection.Right)
```
A wall is either a tile set as a wall or the edge of the scene if a tilemap is set.
* **direction**: the direction the sprite moves toward when detecting a wall collision: `left`, `right`, `top`, or `bottom`.
* a [boolean](/types/boolean) value that is `true` if the sprite is hitting a wall on the chosen side or `false` if not.
Make a tilemap with a wall going from top to bottom. Set a sprite in motion and make it say "Ouch!" when it hits the wall on its `right` side.
```blocks
tiles.setTilemap(tilemap`level1`)
let mySprite = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . 1 1 1 1 1 1 . . . . .
. . . 1 1 2 2 2 2 2 2 1 1 . . .
. . . 1 2 2 2 2 2 2 2 2 1 . . .
. . 1 2 2 2 2 2 2 2 2 2 2 1 . .
. . 1 2 2 2 2 2 2 2 2 2 2 1 . .
. . 1 2 2 2 2 2 2 2 2 2 2 1 . .
. . 1 2 2 2 2 2 2 2 2 2 2 1 . .
. . 1 2 2 2 2 2 2 2 2 2 2 1 . .
. . 1 2 2 2 2 2 2 2 2 2 2 1 . .
. . . 1 2 2 2 2 2 2 2 2 1 . . .
. . . 1 1 2 2 2 2 2 2 1 1 . . .
. . . . . 1 1 1 1 1 1 . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Player)
mySprite.setBounceOnWall(true)
mySprite.vx = 80
mySprite.vy = 70
forever(function () {
if (mySprite.isHittingTile(CollisionDirection.Right)) {
mySprite.sayText("Ouch!", 200, false)
}
})
```
[](/reference/scene/tile-kind-at),
[](/reference/scene/set-tile-at)
```jres
{
"transparency16": {
"data": "hwQQABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
"mimeType": "image/x-mkcd-f4",
"tilemapTile": true
},
"tile1": {
"data": "hwQQABAAAADu7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7g==",
"mimeType": "image/x-mkcd-f4",
"tilemapTile": true,
"displayName": "myTile"
},
"level1": {
"id": "level1",
"mimeType": "application/mkcd-tilemap",
"data": "MTAwYTAwMDgwMDAwMDAwMDAwMDAwMDAwMDEwMDAwMDAwMDAwMDAwMDAwMDAwMTAwMDAwMDAwMDAwMDAwMDAwMDAxMDAwMDAwMDAwMDAwMDAwMDAwMDEwMDAwMDAwMDAwMDAwMDAwMDAwMTAwMDAwMDAwMDAwMDAwMDAwMDAxMDAwMDAwMDAwMDAwMDAwMDAwMDEwMDAwMDAwMDAwMDAwMDAwMDAwMTAwMDAwMDAwMDAyMDAwMDAwMDAwMjAwMDAwMDAwMDIwMDAwMDAwMDAyMDAwMDAwMDAwMjAwMDAwMDAwMDIwMDAwMDAwMDAyMDAwMDAwMDAwMjAwMA==",
"tileset": [
"myTiles.transparency16",
"myTiles.tile1"
],
"displayName": "level1"
},
"*": {
"mimeType": "image/x-mkcd-f4",
"dataEncoding": "base64",
"namespace": "myTiles"
}
}
```