Sprite Tilemap Location with A-Star Path Extension

@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.

1 Like

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

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

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

1 Like

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.


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

1 Like

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


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, 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?

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.

1 Like

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.

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