Hi there folks, here i am creating a small music gadget for my game , here is the code for the module…
package
{
//flash imports....
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.net.*;
import flash.text.*;
import gs.*;
import gs.easing.*;
public class MusicLoader extends MovieClip
{
public var file:String;
public var fileLoader:URLLoader;
public var trackXML:XML;
public var trackList:XMLList;
public var stageRef:Stage;
public var glowColor:uint = 0x669999;
public var panelOpen:Boolean = false;
public var trackLoader:Sound;
public var currentTrack:Number = 0;
private var channel:SoundChannel
public function MusicLoader ():void
{
init ();
}
private function init ():void
{
//initate stage properties
addEventListener (Event.ADDED_TO_STAGE, addToStage);
//instanciate the new song....
trackLoader = new Sound();
channel = new SoundChannel();
//default file to be loaded....!
if (file == null)
{
file = "music.xml";
}
//create loader to load file....
fileLoader = new URLLoader();
fileLoader.load (new URLRequest(file));
fileLoader.addEventListener (Event.COMPLETE, loadComplete);
}
//crate a refrence to stage.....
private function addToStage (eve:Event):void
{
//create a refrence to stage
stageRef = this.stage;
//set Postion..
setPosition (stageRef.stageWidth, stageRef.stageHeight - this.height);
//add general listeners,,,,,
addListeners ();
}
//add general listeners
private function addListeners ():void
{
//listeners to this object only
this.addEventListener (MouseEvent.MOUSE_OVER, glowIt);
this.addEventListener (MouseEvent.MOUSE_OUT, glowIt);
this.addEventListener (MouseEvent.CLICK, openIt);
//listeners for inner buttons...
this.forward_btn.buttonMode = true;
this.forward_btn.addEventListener (MouseEvent.MOUSE_OVER, shadowIt);
this.forward_btn.addEventListener (MouseEvent.MOUSE_OUT, shadowIt);
this.forward_btn.addEventListener (MouseEvent.CLICK, changeTrack);
this.backward_btn.buttonMode = true;
this.backward_btn.addEventListener (MouseEvent.MOUSE_OVER, shadowIt);
this.backward_btn.addEventListener (MouseEvent.MOUSE_OUT, shadowIt);
this.backward_btn.addEventListener (MouseEvent.CLICK, changeTrack);
this.volume_btn.buttonMode = true;
this.volume_btn.addEventListener (MouseEvent.MOUSE_OVER, shadowIt);
this.volume_btn.addEventListener (MouseEvent.MOUSE_OUT, shadowIt);
//this.volume_btn.addEventListener (MouseEvent.CLICK, changeTrack);
}
//roll over and roll out effects
private function glowIt (eve:MouseEvent):void
{
if (eve.type == MouseEvent.MOUSE_OVER)
{
TweenMax.to (eve.currentTarget, 1.5, {glowFilter:{color:glowColor, alpha:1, blurX:15, blurY:15, quality:2}});
}
else if (eve.type == MouseEvent.MOUSE_OUT)
{
TweenMax.to (eve.currentTarget, 1.5, {glowFilter:{color:glowColor, alpha:1, blurX:0, blurY:0, quality:2}});
}
}
//inner button rollOver function...
private function shadowIt(eve:MouseEvent):void
{
if (eve.type == MouseEvent.MOUSE_OVER)
{
TweenMax.to (eve.currentTarget, 1, {dropShadowFilter:{color:0x000000, alpha:0.75, blurX:10, blurY:10, distance:0}});
}
else if (eve.type == MouseEvent.MOUSE_OUT)
{
TweenMax.to (eve.currentTarget, 1, {dropShadowFilter:{color:0x000000, alpha:0, blurX:0, blurY:0, distance:0}});
}
}
//open the panel on click
private function openIt (eve:MouseEvent):void
{
if (! panelOpen)
{
var xPos:Number = Math.round((stageRef.stageWidth - this.width) + 25);
TweenMax.to (this, 1.5, {x:xPos });
panelOpen = true;
}
else
{
TweenMax.to (this, 1.5, {x:stageRef.stageWidth});
panelOpen = false;
}
}
//set postion on screen
public function setPosition (xPos:Number, yPos:Number):void
{
this.x = xPos;
this.y = yPos;
}
//load xml file...
private function loadComplete (eve:Event):void
{
//read the loaded xml file
trackXML = new XML(eve.target.data);
//populate the track list from xml
trackList = new XMLList(trackXML.track);
//load the first song in the list
loadMusic(currentTrack);
}
//load the tracks
private function loadMusic(track:Number):void
{
var trackToLoad:String = trackList[track].toString();
//trackLoader = new Sound();
trackLoader.load(new URLRequest(trackToLoad));
trackLoader.addEventListener(Event.COMPLETE, trackComplete);
trackLoader.addEventListener(ProgressEvent.PROGRESS, trackProgress);
}
//loading in progress
private function trackProgress(eve:ProgressEvent):void
{
this.title_txt.text = "Loading......"
}
//loading complete
private function trackComplete(eve:Event):void
{
channel = new SoundChannel();
channel = trackLoader.play();
this.title_txt.text = (trackLoader.id3.songName + " : " + trackLoader.id3.artist);
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);
}
//when sound is played once...
private function doSoundComplete(eve:Event):void
{
trackLoader.play();
}
//change the current track
private function changeTrack (eve:MouseEvent):void
{
if (eve.currentTarget.name == "forward_btn")
{
currentTrack += 1;
if (currentTrack > trackList.length())
{
currentTrack = trackList.length() - 1;
}
}
if (eve.currentTarget.name == "backward_btn")
{
currentTrack -= 1;
if (currentTrack <= 0 )
{
currentTrack = 0;
}
}
//load the new song...
loadMusic(currentTrack);
}
}
}
now the problem is that when i click next or previous button it displays this Error and doesn’t navigates between next or previous tracks.
Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at MusicLoader/loadMusic()
at MusicLoader/changeTrack()
can some one tell me why is it happening so…