Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by SlinkyDinky

I want to play a sound effect at each step of the player. So far I tried a setInterval:

			}else if(ig.input.state('right')){
				setInterval(this.stepSFX.play(), 1000);
				this.accel.x = accel;
				this.flip = false;

But this doesn't work since the sound effect is played at each tick anyways. Any solutions?

1 decade ago by vincentpiel

you should synchronise the sound with the step of the animation just reached.

Have a look at animation.js : you could inject 'update' so that, if this.frame == a step frame, a sound is played (to be more generic : a callback function is called).

You already have an animation array = [1,1,2,3,4,4,5, 6, 7], you'll need also into this animation an array stating on which frame a sound should be played : expl : stepSound= [ 3, 7], and when stepSound.indexOf(this.frame) != -1 you will play the sound (= you will call the callback).

One way to do it : in the init of your entity :
this.walkAnim = this.addAnim('walk', 0.15, [1,1,2,3,4,4,5, 6, 7] );
this.walkAnim.stepSound = [3, 7 ];
this.walkAnim.stepSoundPlay = function() { /* play the sound */ } ;

and inject in update of Animation.js :
{ this.parent(....);
if (this.stepSound && this.stepSound.indexOf(this.frame) != -1) {
this.stepSoundPlay() }
}

Obviously there are many ways to implement this,
1) the simplest (somehow hacky) is to read this.walkAnim.frame within the entity update and play sound play if need be.
2) you could just change a bit Animation.js like i suggested above.
3) and the most 'clean' way is to create an 'AnimationSound' class that extends from animation adding sound play parameters.
Page 1 of 1
« first « previous next › last »