# How to test when player is over a certain type of tile?

**URL:** https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227
**Category:** Arcade
**Created:** [June 5, 2019, 8:57pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227 "2019-06-05T20:57:13Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ladyada](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/ladyada/32/167_2.png) [@ladyada](https://forum.makecode.com/u/ladyada)
#### Post date: [June 5, 2019, 8:57pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/1 "2019-06-05T20:57:13Z")

</div>

ok for my marble game, there’s holes in the map, when the ball overlaps the hole…game over!  
but im struggling with how to get the logic for either “make these tiles enemy type” OR “place enemy on top of the array of these tiles” OR “test player overlap tile type”

this would be handy for creating adventure maps that have treasure/warpzones/etc? im sure its possible somehow but its been 20 minutes of prodding and i cant figure it out 😃

code in question

> **[marble](https://arcade.makecode.com/55172-48410-69868-34617)**
>
> Made with ❤️ in MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [June 5, 2019, 11:22pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/2 "2019-06-05T23:22:21Z")

</div>

So we have a pretty good way to handle ‘place enemies on these tiles’ that I added a while back and used in the `Jumpy Platformer` example a lot. Here’s a shorter example: [https://makecode.com/\_icoiC94WEV8d](https://makecode.com/_icoiC94WEV8d)

Basically, you want to:

1. Use `array of all {color} tiles` from the scene category to get all the orange (in your case, hole) tiles
2. Iterate over those tiles with the for loop
3. Create a new sprite for the thing to place (e.g. coins, or the holes for you)
4. Place them ontop of the tile in each iteration of the loop with `on top of {myTile} place {mySprite}`

This can be pared down to just the loop without the extra temp variables depending on what you need to store / change: [https://makecode.com/\_6zT36tHWaMyJ](https://makecode.com/_6zT36tHWaMyJ)

I think I’ll add another tutorial to the `Game Design Concepts` to show this approach off a bit more clearly, as it’s fairly useful but not something that would be obvious to do - maybe making a forest and use the tiles to create a path of breadcrumb sprites 🙂

---

<div class="post-metadata">

### Author: ![ladyada](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/ladyada/32/167_2.png) [@ladyada](https://forum.makecode.com/u/ladyada)
#### Post date: [June 6, 2019, 4:01pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/3 "2019-06-06T16:01:54Z")

</div>

thanks - i can do that.  
i do sorta feel like this blockset should do the same, perhaps if the type of the first item is a list it can do the iteration for ya?  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/1X/b6d9a9ffeda4e00a0dee119df96b21405520f7ca.png)

---

<div class="post-metadata">

### Author: ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)
#### Post date: [June 6, 2019, 9:32pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/4 "2019-06-06T21:32:14Z")

</div>

In the current form of Static TypeScript that is used in PXT, functions can’t be overloaded. So, the same function cannot accept two different types for its parameters.

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [June 6, 2019, 11:07pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/5 "2019-06-06T23:07:03Z")

</div>

That’s not quite true, as we can use some of the more advanced features of typescript to allow arbitrary type combinations; for example, `function placeThings(tile: tiles.Tile | tiles.Tile[]) { }` would accept either a tile or a tile array, and then you can use `a instanceOf b` to disambiguate whether tile is a tile or an array:

> **[multi place](https://arcade.makecode.com/52481-00971-93124-52324)**
>
> Made with ❤️ in MakeCode Arcade.

That said, I’m a tiny bit hesitant on the idea here for a few reasons; one, it would make a weird semantic difference between the two (the implication would be that if you place on a single tile it would place that sprite where you want, but if you place on a tile array it would have to clone the sprite and place it on each tile - then what happens to the original sprite? We don’t have any other cases where a sprite is duplicated without the user duplicating it manually).

I think I would personally lean towards specifying `tile` and `sprite` on the block itself (`on top of tile {hole} place sprite {holeSprite}`) to make it a bit more clear, and consider a different approach as a different block as this one just goes to a method on the tile class

---

<div class="post-metadata">

### Author: ![ladyada](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/ladyada/32/167_2.png) [@ladyada](https://forum.makecode.com/u/ladyada)
#### Post date: [June 6, 2019, 11:18pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/6 "2019-06-06T23:18:58Z")

</div>

im up for whatever, just that manually iterating with a loop seems un-makecode-y 🙂 being able to place enemies/treasure with the tilemap would be good!

---

<div class="post-metadata">

### Author: ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)
#### Post date: [June 6, 2019, 11:53pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/7 "2019-06-06T23:53:37Z")

</div>

Oh that’s interesting, @jwunderl. I don’t remember when I wanted to do a function overload (it was in one of the internal functions for one of my extensions), but I’ll have to remember that syntax in the future. I’m with you, though: For the Blocks environment, these two implementations should be two different blocks.

---

<div class="post-metadata">

### Author: ![NightIsland](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nightisland/32/349_2.png) [@NightIsland](https://forum.makecode.com/u/NightIsland)
#### Post date: [September 15, 2019, 6:26pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/8 "2019-09-15T18:26:18Z")

</div>

I’d like to ask for something else, if I may: it’s not always practical to create a sprite for every type of a tile; for instance, in a game that has a lot of water and a lot of land, detecting whether you’re flying over land would be very silly to create a sprite for every land tile.

So I’d like to return to the original question, and ask ‘how can I detect what type of tile is at position X,Y [or underneath an existing sprite’s position, or whatever)?’

It would be perfectly acceptable, for my purposes, to be able to enquire of the tile map what tile is at a given position - we have a block to acquire the tile at the co-ordinates, we just can’t tell anything about it at all. Even if we had access to the colour index and could compare it to something else, that would be absolutely fine.

**…and I’ve just found I can hack this by reading myTile.tileSet, which is an integer, and using that as a colour index. In case anyone else needs it.**

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [September 15, 2019, 7:13pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/9 "2019-09-15T19:13:53Z")

</div>

Yup, the `.tileSet` is property of the tile object you want (I wouldn’t really call it a hack necessarily as it was written exactly for that purpose, it’s just a part of the typescript API that doesn’t currently have a block associated with it).

---

<div class="post-metadata">

### Author: ![NightIsland](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nightisland/32/349_2.png) [@NightIsland](https://forum.makecode.com/u/NightIsland)
#### Post date: [September 15, 2019, 7:25pm UTC](https://forum.makecode.com/t/how-to-test-when-player-is-over-a-certain-type-of-tile/227/10 "2019-09-15T19:25:13Z")

</div>

Ah! I used the word ‘hack’ because I couldn’t find any reference to it anywhere, so didn’t know if it was an internal variable I’d lucked onto or not 🙂

I’m deeply pleased you included it though - thank you!
