PDA

View Full Version : [AS2] sound


Adam_C
07-01-2009, 05:34 PM
i have this code:

if (Key.isDown(Key.SPACE)) {
reload.start(0, 0);
}


but how do i get the sound to play only once when i press the space bar?


*note: the sound does play, only it will repeat if you hold down the space bar.

:)

CyanBlue
07-01-2009, 05:42 PM
The second parameter for the start() function is the one you specify how many times you want the sound to loop... So, set it to 1 and see if that works for you...

Adam_C
07-01-2009, 05:45 PM
The second parameter for the start() function is the one you specify how many times you want the sound to loop... So, set it to 1 and see if that works for you...

thanks for your reply but i have already tried this and it does not work?

attunedesigns
07-01-2009, 05:54 PM
The quick fix is to use onKeyUp which isn't dispatched constantly.. but here is with onKeyDown:

var s:Sound = new Sound();
s.attachSound('hit');
s.onSoundComplete=function(){soundPlaying=false};

var soundPlaying:Boolean;
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (Key.isDown(Key.SPACE) && !soundPlaying) {
soundPlaying=true;
s.start(0,0);
}
};
Key.addListener(keyListener);

onEnterFrame=function(){trace(soundPlaying)};


How you like that?

Adam_C
07-01-2009, 06:01 PM
it doesn't work, but don't worry about it now :P

thanks for your help (Y) :)

CyanBlue
07-01-2009, 06:02 PM
If you have a sound file with its linkage identifier set up as 'shotgun', this code play the sound only once...
//
var snd:Sound = new Sound();
snd.attachSound("shotgun");

var keyListener_obj:Object = new Object();
keyListener_obj.onKeyDown = function()
{
switch (Key.getCode())
{
case Key.SPACE:
{
snd.start(0, 1);
break;
}
}
}
Key.addListener(keyListener_obj);

attunedesigns
07-01-2009, 06:07 PM
I just tested out the code i posted, and it does work. It will not be allowed to play again until the first instance ends.

And CyanBlue, I think he was worried about holding down the key.