Skip to main content
Participating Frequently
November 14, 2010
Answered

Play gun sound while key is pressed.

  • November 14, 2010
  • 1 reply
  • 1027 views

Hello,

I want my uzi sound to play when space is pressed and I know how to do that, but

I want the sound to play for 2 frames then the same sound should play again and stop the first one so it starts from the beginning when space is pressed down. Otherwise they just overlap eachother and it will sound wierd.
And when the key is released the sound that's already beginned should play to it's finnish.

Thank you.
Sorry for my english.

Message was edited by: antworker

This topic has been closed for replies.
Correct answer kglad

i assume that code is in a loop.

create a new movieclip, attach your uzi sound to its first first frame, set your sound's sync mode to stream at a 2nd non-keyframe and drag the movieclip to the stage of the timeline that contains your code.  assign the movieclip an instance name, eg uzisound_mc.

you can then change your code to:

if(Key.isDown(Key.SPACE))
{
uzisound_mc.play()
} else {
uzisound_mc.stop();
}

1 reply

kglad
Community Expert
Community Expert
November 14, 2010

copy and paste the code you use to play your uzi sound.

antworkerAuthor
Participating Frequently
November 14, 2010
if(Key.isDown(Key.SPACE))
{

shoot = new Sound(this);
shoot.attachSound("uzi");
shoot.start(0, 0);


}

This is what I use to play the sound.

This script is written on frame 1.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
November 14, 2010

i assume that code is in a loop.

create a new movieclip, attach your uzi sound to its first first frame, set your sound's sync mode to stream at a 2nd non-keyframe and drag the movieclip to the stage of the timeline that contains your code.  assign the movieclip an instance name, eg uzisound_mc.

you can then change your code to:

if(Key.isDown(Key.SPACE))
{
uzisound_mc.play()
} else {
uzisound_mc.stop();
}