# Sprite Tilemap Location with A-Star Path Extension

**URL:** <https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814>\
**Category:** Help\
**Created:** [December 5, 2020, 8:33pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814 "2020-12-05T20:33:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![timber](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@timber](https://forum.makecode.com/u/timber)\
**Post date:** [December 5, 2020, 8:33pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/1 "2020-12-05T20:33:01Z")

</div>

@jwunderl I am using your a-star path extension, and I want a sprite to follow a path to another sprite. Both sprites will be moving around a tilemap, so their locations will constantly be updated. Is this something the a-star extension can handle? I can’t figure out a way to find the column and row location of both sprites.

---

<div class="post-metadata">

**Author:** ![timber](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@timber](https://forum.makecode.com/u/timber)\
**Post date:** [December 5, 2020, 10:12pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/2 "2020-12-05T22:12:44Z")

</div>

I did discover that you can divide a sprite’s x and y coordinates and divide both by 16 to get a rough column and row location

---

<div class="post-metadata">

**Author:** ![felixtsu](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/felixtsu/32/12024_2.png) [@felixtsu](https://forum.makecode.com/u/felixtsu)\
**Post date:** [December 6, 2020, 7:27pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/3 "2020-12-06T19:27:48Z")

</div>

The tilemap ext provides useful helper functions to calculate row / column index of a sprite

> **[microsoft/pxt-tilemaps](https://github.com/microsoft/pxt-tilemaps/)**
>
> MakeCode Arcade extension for handling multiple tilemaps and extra tilemap blocks - beta - microsoft/pxt-tilemaps

Dividing x/y by 16 works too in tile size of 16pixel tilemaps.

---

<div class="post-metadata">

**Author:** ![cosmoscowboy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cosmoscowboy/32/1718_2.png) [@cosmoscowboy](https://forum.makecode.com/u/cosmoscowboy)\
**Post date:** [December 6, 2020, 7:28pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/4 "2020-12-06T19:28:47Z")

</div>

The tilemap is created in multiples of 16x16 pixels. So if you are wanting to set a sprite at a certain row/column location, you could add a function that takes a sprite, row and column value, and sets the sprite’s x/y based on the input.

(I am just writing the pseudocode, not necessarily the block or script code)

```
function setSpriteLocation(sprite, column, row)
  sprite.setx(column x 16)
  sprite.sety(row x 16)

```

You could also check if the row/column values are less than 0, and set those values to zero. This does, however, set the sprite to be at the bottom/right corner of a row/column location (i.e. sprite, 1, 1 = sprite.x = 16, sprite.y = 16) Otherwise if you are really wanting to place a sprite exactly at the middle of the row & column, you could subtract 8 from each value calculated.

You could also use the ‘Sprite Grid’ & ‘tilemap’ extensions for more control over where you want to place the sprite, plus you get extra functions when working with a tilemap.

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/a/a56f2c967039c1dd7c8b19ee542c6c1e17fe6eb3.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/a/a40e7fbc13f5bfbe22f7e61d72a19c35c12a4911.png)

You can find them under the ‘Advanced’ block menu and the option ‘+ Extensions’

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/0/0ea6b01fecd26e9d90619f9263b1a29f42cd9dd4.png)

---

<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:** [December 7, 2020, 7:53pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/5 "2020-12-07T19:53:56Z")

</div>

Following other sprites this way is possible / as mentioned the pxt tilemaps extension is probably the one you want to get the row / col for sprites, but just a general note on perf – depending on the size of the tilemap / number of sprites that are following this could very easily make perf miserable on hardware (simulator will most likely be fine though!) – long term I might be able to optimize a bit / rewrite a bit in c++ to speed things up, but it won’t necessarily ever be super fast on hw

---

<div class="post-metadata">

**Author:** ![cosmoscowboy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cosmoscowboy/32/1718_2.png) [@cosmoscowboy](https://forum.makecode.com/u/cosmoscowboy)\
**Post date:** [December 7, 2020, 10:09pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/6 "2020-12-07T22:09:07Z")

</div>

Are you saying that the ‘ptx tilemaps’ extension degrades performance, or that tilemaps in general can be sluggish when it is large and/or contains many sprites?

I posted an issue some time ago, [Help with floating sprites](https://forum.makecode.com/t/help-with-floating-sprites/4408/4), where Richards mentions that large tilemaps, perhaps the number of sprites, and scrolling around, but I am not sure what I need to do to stop the sprites floating. I can make the tilemap smaller and have less sprites, but whatever I do the problem still occurs (even on a mobile phone). I don’t feel like I have a definite explanation on what is causing the problem. Would you be able to elaborate?

---

<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:** [December 7, 2020, 10:17pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/7 "2020-12-07T22:17:09Z")

</div>

I was referring to the proposed ‘make sprite follow another with tilemap by constantly using a\*’, the a\* implementation on hardware is a bit resource intensive. Tilemaps in general are a bit fast.

For your game it looks like it’s mainly just the number of enemy sprites monotonically increasing that’s causing the perf issue; there’s always perf work we can do / will when we get time, but hardware will always have a cap on how many sprites can be efficiently handled without lag. I’m gonna look at fixing the floating issue when perf is bad for the next major release though as we’ve seen that in a few cases / something is clearly off there.

---

<div class="post-metadata">

**Author:** ![cosmoscowboy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cosmoscowboy/32/1718_2.png) [@cosmoscowboy](https://forum.makecode.com/u/cosmoscowboy)\
**Post date:** [December 7, 2020, 10:34pm UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/8 "2020-12-07T22:34:07Z")

</div>

I have only noticed the issue occurring with a tile map at this stage. In my latest game, that does not use a tilemap, I don’t have any issues with the number of sprites and it works really well on my Pygamer.

> [@Shark Frenzy](https://forum.makecode.com/t/shark-frenzy/4787):
>
> I am finally ready a have submitted my #featured-game #game for the Ocean Jam: Please let me know if you find any bugs or issues running this on handheld devices. I look forward to your feedback. Feel free to take any code/ideas from the game; you can find the source here: and you can play the game full screen in a browser here:

---

<div class="post-metadata">

**Author:** ![timber](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@timber](https://forum.makecode.com/u/timber)\
**Post date:** [December 11, 2020, 5:52am UTC](https://forum.makecode.com/t/sprite-tilemap-location-with-a-star-path-extension/4814/9 "2020-12-11T05:52:05Z")

</div>

Thank you everyone for the info. I really appreciate it.
