I want to play a sound when my player walks

i want to play a sound when my player walks but i don't know how to

Frist, make a sound and give it a name. For my example I will name the sound "walkingsound"

In the script for "player" add an update section if there is not one already and add the sound call to it.

on update do
  sound "walkingsound"
end

This will make the sound happen every time the user presses the dpad while not in a menu or something.
Here is how you make it so it only makes a sound if they actually move. (Because the update routine happens whenever they press the dpad even if they press it trying to walk into something solid.)

on update do
  if event.tx==event.px then
    if event.ty==event.py then
      sound "walkingsound"
    end
  end
end

event.tx & event.ty are the location of where the player TRIED to move to; event.px & event.py are the location of where the player IS CURRENTLY. If these are the same, that means where they tried to move and where they are currently are the same thing ergo they moved successfully.

If you also wanted to make a sound when they try to move, but they couldn't (probably because there is a solid tile in their way) you have to do something a little trickier since there is no AND operator. Here's what I usually do:

on update do
  and = 0
  if event.tx==event.px then
    and++
  end
  if event.ty==event.py then
    and++
  end
  if and==2 then
    sound "walkingsound"
  else
    sound "bumpsound"
  end
end

or something like that

2 Likes

thank you this helped a lot

is there a version that doesn't have "on update do " because there's another script that begins with the same thing

Well, you can have update call another function, to keep your code clean.

on update do
call "footstep logic"
//do what you need to here.
end

on footstep_logic do
and = 0
  if event.tx==event.px then
    and++
  end
  if event.ty==event.py then
    and++
  end
  if and==2 then
    sound "walkingsound"
  else
    sound "bumpsound"
  end
end
1 Like

is there a version that doesn't contain "on update do" ?

hello,

the "on update do" part of your code is an important thing that triggers each time the player uses the dpad while moving around rooms. it sounds like you already have some code in there, but this is an easy thing to fix.

the solution is not to put the walking sound code in some other function, but to merge your two functions together.

when the player presses the dpad, the game will go through each thing in "update" section of the player in order: top to bottom. whatever code you have in there now, you probably have because there is some other functionality you want to happen each time someone moves (just like the sound). you can take the code I gave you and put it at the beginning or the end of the code you have in update already. the beginning is probably recommended in case you have some code in the rest of the update block that uses the done function or something like that which would make it so the sound code is never reached (unless you'd like that to happen in certain cases.)