Log In  

i am creating some sort of wolfenstein top down testing level. i made a control where it plays a sound that is supposed to be you firing your pistol but i dont want it to constantly play so i need to know how to make a sound delay script if you can tell me i would appreciate it!

P#61244 2019-01-26 22:39

What do you mean exactly? How do you want to delay the sound?

Do you mean that the sound plays every frame that you're shooting? That's different from needing to delay it.

P#61266 2019-01-27 18:14

it plays as in constantly every time i hold the key down, and i want it to play after a certain time, like a fourth of a second. i googled something up on where the carts are and i found out. so drag the photo of this linked cart picture into your carts folder to see the test game and where i'm at. https://www.lexaloffle.com/bbs/files/34330/wolf%20top%20down%20test.p8.png

P#61267 2019-01-27 18:15 ( Edited 2019-01-27 18:55)
1

So those are two different questions:

  1. How do you make it so that the sound effect doesn't play every frame while the key is held down?

This happens because you have this check:

if (btn(❎)) sfx(1)

Now, btn() will be true every frame that the button is being held down. You could perhaps look at btnp() and see if that suits you; that one is only true for the first frame that the button is pressed, and then if it's still held down, again after 15 frames and then every four frames after that. That probably won't fit with the movement, but perhaps with your gun firing?

  1. How do you delay the sound effect for a fourth of a second?

Use a timer. This is a simple example:

function _init()
  fired=false
  fire_timer=0
end

function _update()
  if (fired) fire_timer+=1
  if fire_timer==8 then
    sfx(1)
    fired=false
    fire_timer=0
  end
-- ...
  if (btn(❎) and not fired) fired=true
end

This will play the sound effect after about 1/4 of a second. Since you're running at 30 fps, 1/4 of that is 7.5 frames, so I just picked 8. (60 fps would be divisible by 4, of course.)

This is obviously a crude example. Depending on how much logic you want to put into the bullet firing, you might want to wrap all this into a fire_gun() function or something, but hopefully this gives you some ideas.

P#61268 2019-01-27 19:49 ( Edited 2019-01-27 19:50)

thanks! i appreciate it

P#61269 2019-01-27 19:50

bumping this useful thread!

P#117765 2022-09-21 16:26

[Please log in to post a comment]