I have been trying to code in a snapping mechanic in a rhythm maze game that I'm working on, but I can't get it to work. The player always ends up at one of the corners of the map or doesn't snap onto the map tiles. I've been using
flr() |
but it's not working in ANY way. I want the player to snap onto a map tile when they're not moving, can someone please help me with this?



Have you tried x=8*flr(x/8)
? (using x coordinate as an example to show the formula more clearly)



Err...it should? You might need to adjust the position first to tweak it, though. To explain, the map tiles are 8x8 pixels, so to snap to them, you just need to set the position in pixels to a multiple of 8. Assuming you are using pixels for the position, dividing by 8, rounding to an integer, then multiplying by 8 does that. Just using flr()
on the position would snap to the nearest pixel (which wouldn't matter since the drawing functions do that anyway). It doesn't guarantee that the tile would be the correct one though. If you're using 8x8 sprites for the player, adding 4 to the x and y before dividing would make the map tile that gets snapped to more accurate.



You can also cut down a bit on code size and token use by using the floor divide operator "\", as in the following:
Newx=x\8*8 |
[Please log in to post a comment]