I've been using this for my project and I thought I'd post a few additions I've made to it.
My code allows the player to go partially 'behind' foreground objects, as well as animates their movement a tiny bit.
on enter do
// the calculateMove function sets the head's position, and we want that to
// be drawn even before the player moves in a new room; as soon as they enter
call "calculateMove"
end
on update do
// we abstracted the movement calculation into the calculateMove function
// because it needs to be called more than once here
call "calculateMove"
// change player's facing position
// horizontal movement
if event.dx<0 then // if the attempted movement on the X axis was negative
playerDirection = "left" // save this so we can use it
elseif event.dx>0 then
// same basic logic as above
playerDirection = "right"
end
// Check if the player is behind any foreground objects
call "checkisbehind"
// Make the character play a walking animation if they aren't behind anything
call "makewalk"
end
on draw do
// the "player" (the feet) gets drawn automatically;
// here, we manually draw the tile called "player head"
draw "player head {playerDirection}" at playerHeadX,playerHeadY
end
on checkisbehind do
// check if the player is going behind something
// first set a 'isbehind' variable to 0. If we find he is behind something, set it to 1.
// this is used to stop the code below overwriting what we are doing here.
isbehind = 0
thetile = name event.px,event.py
if thetile=="outer wall h" then
swap "outer wall h"
isbehind = 1
elseif thetile=="table top left" then
swap "player {playerDirection} table top left"
isbehind = 1
elseif thetile=="table top mid" then
swap "player {playerDirection} table top mid"
isbehind = 1
elseif thetile=="table top right" then
swap "player {playerDirection} table top right"
isbehind = 1
elseif thetile=="tv top left" then
swap "player {playerDirection} tv top left"
isbehind = 1
elseif thetile=="tv top mid" then
swap "player {playerDirection} tv top mid"
isbehind = 1
elseif thetile=="tv top right" then
swap "player {playerDirection} tv top right"
isbehind = 1
elseif thetile=="inner wall top" then
swap "player {playerDirection} inner wall top"
isbehind = 1
elseif thetile=="inner wall top left" then
swap "player {playerDirection} inner wall top left"
isbehind = 1
elseif thetile=="inner wall top right" then
swap "player {playerDirection} inner wall top right"
isbehind = 1
elseif thetile=="umbrella stand top" then
swap "player {playerDirection} umbrella stand top"
isbehind = 1
end
end
on makewalk do
if isbehind==0 then
swap "player walk {playerDirection}"
// then, switch to static image
keeptile = thetile
wait 0.1 then
if keeptile==thetile then
swap "player {playerDirection}"
end
end
end
end
on calculateMove do
playerHeadX = event.px // event.px is where the player moved
playerHeadY = event.py
playerHeadY -= 1 // the head is one tile above
end