Footsteps sound effect
childski

Hello,

I am trying to implement a footsteps sound effect in my 2D RPG game.
The audio file that I currently have is the sound of 2 footsteps only.

Currently, I have this function in my game to control the player:

#SelectExpand
1if(state != IDLE) 2 { 3 active = true; 4 if(al_key_down(&keystate, ALLEGRO_KEY_DOWN)) 5 { 6 character1.y += movespeed; 7 dir = DOWN; 8 al_play_sample(footsteps,1,0,1,ALLEGRO_PLAYMODE_ONCE,NULL); 9 } 10 else if(al_key_down(&keystate, ALLEGRO_KEY_UP)) 11 { 12 character1.y -= movespeed; 13 dir = UP; 14 al_play_sample(footsteps,1,0,1,ALLEGRO_PLAYMODE_ONCE,NULL); 15 } 16 else if(al_key_down(&keystate, ALLEGRO_KEY_RIGHT)) 17 { 18 character1.x += movespeed; 19 dir = RIGHT; 20 al_play_sample(footsteps,1,0,1,ALLEGRO_PLAYMODE_ONCE,NULL); 21 } 22 else if(al_key_down(&keystate, ALLEGRO_KEY_LEFT)) 23 { 24 character1.x -= movespeed; 25 dir = LEFT; 26 al_play_sample(footsteps,1,0,1,ALLEGRO_PLAYMODE_ONCE,NULL); 27 } 28 else 29 active = false;

I have tried to use ALLEGRO_PLAYMODE_LOOP, but it loops the sound effect very slowly (like a 1 second break before the sound effect loops itself).
And obviously it only plays once since I used ALLEGRO_PLAYMODE_ONCE.

Is there any better way to implement this sound effect?

Thank you so much!

Paulinek13

Hi,

One solution may be like this:

#SelectExpand
1// ... 2 3case ALLEGRO_EVENT_TIMER: 4{ 5 // ... 6 7 if(!(al_get_timer_count(timer) % 12)) // adjust the number 8 { 9 switch(runner.state) 10 { 11 case RUNNER_STATE::WALK_L: 12 case RUNNER_STATE::WALK_R: al_play_sample(sample_runner_steps, .5f, 0.f, 1.f, ALLEGRO_PLAYMODE_ONCE, 0); break; 13 14 default: break; 15 } 16 } 17 // ... 18} 19 20// ...

I'm not sure if it is actually better, but it works fine for me.

childski

@Paulinek13: hi, thanks for your input! I have tried several methods using a timer but it still doesn't work properly. It only plays once every 1~2 seconds.

Paulinek13

Now I guess that your footsteps sample lasts,
let's say, 2 seconds, but the actual sound takes only
1 secon, or something, and there is a relatively
long empty gap.
Maybe you should try to cut your sample a little
so that there will not be any silent break?
:)

pmprog

Just something else to consider - how long does it take for your game loop take?

Also, what options did you select when creating your voice and mixer?

Edgar Reynaldo

You could loop the sample, and pause it when they are not walking.

Are you trying to sync the sample with your player's footsteps?

Thread #618166. Printed from Allegro.cc