Skip to main content
Known Participant
November 26, 2013
Question

How can I stop a sound from looping so it plays only once each time the up key is pressed?

  • November 26, 2013
  • 1 reply
  • 2814 views

Hi there here is my code, the basis is it is playing a mp3 from my library.

Basically as the topic suggests I only want the sound to play once each time the UP arrow key is pressed. At the moment when you hold the up arrow down the sound keeps triggering and looping and gets really loud and noisy.

Here is my code for my game:

package com.chasegame.components

{

          import flash.display.MovieClip;

          import flash.display.Stage;

          import flash.events.Event;

          import com.tech.utils.KeyObject;

          import flash.ui.Keyboard;

          import flash.media.Sound;

          import flash.media.SoundChannel;

          public class SpaceShip extends MovieClip

          {

                    private var key:KeyObject;

                    private var speed:Number = 0.3;

                    private var rotateSpeed:Number = 5;

                    private var vx:Number = 0;

                    private var vy:Number = 0;

                    private var friction:Number = 0.95;

                    private var shipSound:roar = new roar();

                    private var shipSoundChannel:SoundChannel = new SoundChannel();

                    public function SpaceShip () : void

                    {

                                        key = new KeyObject(stage);

                                        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

                    }

                    public function loop(e:Event) : void

                    {

                                        if (key.isDown(Keyboard.UP))

                                        {

                                                       vy += Math.sin(degreesToRadians(rotation)) * speed;

                                                       vx += Math.cos(degreesToRadians(rotation)) * speed;

                                                       shipSoundChannel = shipSound.play();

                                        }

                         else

                         {

                                                       vy *= friction;

                                                       vx *= friction;

                                        }

                              if (key.isDown(Keyboard.RIGHT))

                                        rotation += rotateSpeed;

                              else if (key.isDown(Keyboard.LEFT))

                                        rotation -= rotateSpeed;

                              y += vy;

                              x += vx;

                              if (x > stage.stageWidth)

                                        x = 0;

                              else if (x < 0)

                                        x = stage.stageWidth;

                              if (y > stage.stageHeight)

                                        y = 0;

                              else if (y < 0)

                                        y = stage.stageHeight;

                    }

                    public function degreesToRadians(degrees:Number) : Number

                    {

                              return degrees * Math.PI / 180;

                    }

          }

}

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
November 26, 2013

If you want the sound to only play once when the key is pressed then you don't want to have it in the ENTER_FRAME event handler like you have it.  It is starting new sounds at the frame rate of your file the way you have it now.

Try having an event listener dedicated to a KeyboardEvent.KEY_DOWN event where you detect when the UP key gets pressed and use that to trigger playing the sound.