Jump to content

AS3: Dynamic Variable Names


Cryzabey
 Share

Recommended Posts

I'm attempting to play songs dynamically in AS3, but I'm having a little bit of trouble. At the very bottom I've included the portion of my script that focuses around the audio.The audio plays if:

songPlayer = song1.play();

But not if:

var playMe = ["song"+currentSong".play();"];			trace(playMe);			songPlayer = [playMe].play();

Basically, I can't get it to play my audio unless I specify the sound. Any attempts at trying to make it dynamic are less than fruitful. Any/all help is appreciated [:

//// createMusicPlayer()//// Creates the music player, and sets up the events// that control how the main BG song is played.//		public var currentSong:int = 1;		public var lastPosition:Number = 0;		public var isPlaying:String = "true";		public var stopBTN:SimpleButton = new SimpleButton();		public var playBTN:SimpleButton = new SimpleButton();		public var pauseBTN:SimpleButton = new SimpleButton();		public var skipForwardBTN:SimpleButton = new SimpleButton();		public var SkipBackwardBTN:SimpleButton = new SimpleButton();		public var playlist:Array = new Array("song1", "song2", "song3", "song4");		public function createMusicPlayer() {						// Setting up song settings			songSettings.volume = 0.5;			songPlayer.soundTransform = songSettings;						// Creating and placing music controls			pauseBTN = new BT_MusicPause;			pauseBTN.x = 550;			pauseBTN.y = 20;			addChild(pauseBTN);			stopBTN = new BT_MusicStop;			stopBTN.x = (pauseBTN.x + 70);			stopBTN.y = 20;			addChild(stopBTN);									// Adding event listeners			stopBTN.addEventListener(MouseEvent.CLICK, onClickStop);			pauseBTN.addEventListener(MouseEvent.CLICK, onClickPause);						var playMe = ["song"+currentSong".play();"];			trace(playMe);			songPlayer = [playMe].play();		}//// onClickStop()//		function onClickStop(e:MouseEvent):void{						// Stop			isPlaying = "false";			lastPosition = 0;			songPlayer.stop();						// Switch out buttons			if(pauseBTN.hasEventListener(MouseEvent.CLICK)){				playBTN = new BT_MusicPlay;				playBTN.x = pauseBTN.x;				playBTN.y = 20;				removeChild(pauseBTN);				addChild(playBTN);				playBTN.addEventListener(MouseEvent.CLICK, onClickPlay);				pauseBTN.removeEventListener(MouseEvent.CLICK, onClickPause);			}		}//// onClickPause()//		function onClickPause(e:MouseEvent):void{						// Save position and stop			isPlaying = "false";			lastPosition = songPlayer.position;			songPlayer.stop();						// Switch out buttons			playBTN = new BT_MusicPlay;			playBTN.x = pauseBTN.x;			playBTN.y = 20;			removeChild(pauseBTN);			addChild(playBTN);			playBTN.addEventListener(MouseEvent.CLICK, onClickPlay);			pauseBTN.removeEventListener(MouseEvent.CLICK, onClickPause);		}//// onClickPlay()//		function onClickPlay(e:MouseEvent):void{						// Stop, then resume/play			isPlaying = "true";			songPlayer.stop();			songPlayer = song1.play(lastPosition);			songPlayer.soundTransform = songSettings;						// Switch out buttons			pauseBTN = new BT_MusicPause;			pauseBTN.x = playBTN.x;			pauseBTN.y = 20;			removeChild(playBTN);			addChild(pauseBTN);			playBTN.removeEventListener(MouseEvent.CLICK, onClickPlay);			pauseBTN.addEventListener(MouseEvent.CLICK, onClickPause);		}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...