Easy-To-Use Moving Sprite/Item Code

I've seen a lot of people asking how to move tiles, and how to make them not destroy the tile they used to be on in the process. I decided to make a catch-all piece of code that does just that, this code can also be copied over to tiles without the hassle of renaming things in the script. Here it is:

on interact do
tile_name = event.tile
tile_x = event.x
tile_y = event.y
tile_x += 1
tile_pos_solid = solid tile_x,tile_y
if tile_pos_solid==0 then
play "{prev_tile}"
prev_tile = name tile_x,tile_y
tell tile_x,tile_y to
play "{tile_name}"
end
end
end

This code ONLY works if you first do this in the "load" event:

on load do
prev_tile = "white"
end

Note: the load event must be called in the game or player's code. And the "interact" event would need to be replaced with "collect" for an item tile

You could also make the tile leave a tile of your choosing behind every time it moves. By modifying the "prev_tile" variable to the tile name of your choosing. Example that leaves black tiles behind it when moving:

on interact do
tile_name = event.tile
tile_x = event.x
tile_y = event.y
tile_x += 1
tile_pos_solid = solid tile_x,tile_y
if tile_pos_solid==0 then
play "{prev_tile}"
prev_tile = "black"
tell tile_x,tile_y to
play "{tile_name}"
end
end
end

Currently the code only makes the sprite/item tile move to the right, with collision. But this can be changed to any direction, experiment with it.

2 Likes