How can I stop a sound from looping so it plays only once each time the up key is pressed?
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;
}
}
}
