Start eventsong automatisk - Flash / Actionsscript 3
Jeg sidder med en music_controller i actionsscript 3, men vil gerne have at den starter automatisk når den bliver spillet, koden ser sådan her ud:var song:Song = new Song();
var myChannel:SoundChannel = new SoundChannel();
var soundVol:SoundTransform = new SoundTransform();
play_btn.buttonMode = true;
stop_btn.buttonMode = true;
play_btn.addEventListener(MouseEvent.CLICK,startPlay);
stop_btn.addEventListener(MouseEvent.CLICK,stopPlay);
function startPlay(event:MouseEvent):void {
myChannel=song.play(0,10);
play_btn.removeEventListener(MouseEvent.CLICK,startPlay);
}
function stopPlay(event:MouseEvent):void {
myChannel.stop();
play_btn.addEventListener(MouseEvent.CLICK,startPlay);
}
// Code that handles the sound volume
var ratio_volume:Number;
var trackBounds:Rectangle = track_mc.getBounds(track_mc);
var xPos:Number = trackBounds.x;
var yPos:Number = trackBounds.y;
var widthPos:Number = trackBounds.width-track_mc.slider_mc.width;
var heightPos:Number = 0;
var bounds:Rectangle = new Rectangle(xPos,yPos,widthPos,heightPos);
track_mc.slider_mc.x = widthPos;
track_mc.mouseEnabled = false;
track_mc.slider_mc.buttonMode = true;
track_mc.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN,dragSlider);
stage.addEventListener(MouseEvent.MOUSE_UP,stopSlider);
function dragSlider(event:MouseEvent):void {
event.target.startDrag(false,bounds);
addEventListener(Event.ENTER_FRAME,setVolume);
}
function stopSlider(event:MouseEvent):void {
track_mc.slider_mc.stopDrag();
removeEventListener(Event.ENTER_FRAME,setVolume);
}
function setVolume(event:Event):void {
ratio_volume = track_mc.slider_mc.x/widthPos;
soundVol.volume = ratio_volume;
myChannel.soundTransform = soundVol;
}