Hello,
I've created a MovieClip .as class for a MovieClip containing 7 buttons. I'm using an array of these buttons and array of frame labels on the parent MovieClip's timeline to navigate the labels.
...
private function clickHandler(event:MouseEvent):void
{
//Sets the current button listed in the button array
currentButton = MovieClip(event.currentTarget);
index = buttonArray.indexOf(currentButton);
//Uses the index variable to find the corresponding frame label from the label array
MovieClip(root).gotoAndStop(labelArray[index]);
}
...
This works perfectly.
Now, I have another MovieClip .as class I've created for a simple mp3 player MovieClip. I'm pulling in an xml file of each mp3's data including URL, etc... Which all works perfectly.
I'm trying to use the "index" variable from the Nav.as class in the AudioPlayer.as class to play the corresponding number of the song. By calling "playSong(index);"
"playSong(index);" works in a test file when I tested the navigation arrays and corresponding xml number concept. I need to pull the "index" variable from Nav.as to AudioPlayer.as.
...
private function playSong(mySong:Number):void
{
var myURL = my_songs[mySong].@URL;
if (my_channel) {
my_channel.stop();
}
my_sound = new Sound();
my_sound.load(new URLRequest(myURL));
my_channel = my_sound.play();
}
private function onPlay(e:MouseEvent):void {
playSong(index);
}
This works perfectly when tested. For example: "playSong(2)" or "playSong(5)" plays the correct number song.
Thank you in advance!
Is the first and second class in the same flash document or are they 2 separate objects on the same webpage? I'm trying to identify how you'll communicate, directly between classes or if you need to bridge it with JavaScript and ExternalInterface.
ExternalInterface is pretty trivial. Your class will run the "call" method on JavaScript which in turn will send that information to the other Flash object which can read the data passed to it (the index) and act appropriately. Plenty of examples on the Adobe page to show you AS->JS and JS->AS.
If they're both in the same document then proper class communication and application structure is a different topic and we'd need to know how you structured your document before making recommendations.
So you have a webpage with 2 different Flash objects embedded in it? If so you'll need to click on the ExternalInterface link and take a look at the sample code.
Once you get the index number you need you'll simply call a JavaScript function and tell it the index. JavaScript will then communicate with the mp3 player, relaying that number. You won't need any class->class communication for this method. This is Flash->Web Page->Flash communication.
ExternalInterface:
Quick and simple minimum code example of sending data from AS->JS and JS->AS (full tutorial linked at top of example):
http://circlecube.com/wp-content/uploads/2010/12/as3-javascript/
I have 9 labels on the timeline: nav and composer 1 - 8 (nav, composer 1, composer 2, etc...) spread out over 10 frames each
The nav movieclip only shows for the first 10 frames I've set for the nav label. The audioPlayer movieclip shows up for the rest of the frames for each composer.
So both movieclips are on the main timeline just not at the same time.
Just add a variable on your main timeline like:
var currentIndex:int;
That will continue to exist throughout the timeline. When the player needs to read it, it can access it via Object(this.parent).currentIndex. Sometimes classes just need a "middle man" and all you're interested in is a very small piece of data, a number.
This makes sense. So, I've added "var currentIndex:int;" to my main timeline and the following is returning "1120: Access of undefined property currentIndex.":
AudioPlayer.as
private function onPlay(e:MouseEvent):void
{
playSong(currentIndex);
}
Nav.as
currentIndex = buttonArray.indexOf(currentButton);
MovieClip(root).gotoAndStop(labelArray[currentIndex]);
So I declared "var currentIndex:int;" in the top of both of my .as files. The nav is working fine but the audioPlayer is playing the first song no matter which composer I click on. The variable is not getting passed to the main timeline from Nav.as and not getting pulled from the main timeline to AudioPlayer.as
You shouldn't need a currentIndex in each class, you just need it on the parent of the other 2 classes. When you say "this.parent" you're accessing the parent display object. That's where you want currentIndex to exist. If both classes are on the same display list then "this.parent" will be the same parent, and therefore the same variable.
I didn't know what kind of display object the parent container was (MovieClip, Sprite, etc), so I referenced it loosely as: "Object(this.parent).currentIndex"
Nav.as should be setting:
Object(this.parent).currentIndex = buttonArray.indexOf(currentButton);
AudioPlayer.as should be reading it via:
playSong(Object(this.parent).currentIndex);
I've added "public var currentIndex:int;" to my document class (Main.as) and made your suggested edits to both of my .as files. AudioPlayer is now playing the correct song per composer.
However, Nav.as is returning the error ".../Nav.as, Line 83 1120: Access of undefined property currentIndex."
Object(this.parent).currentIndex = buttonArray.indexOf(currentButton);
//The following line is line 83
MovieClip(root).gotoAndStop(labelArray[currentIndex]);
Any time you access currentIndex you must make sure you know where you are. If this line of code is in the Nav.as, then it needs to be updated like so:
MovieClip(root).gotoAndStop(labelArray[Object(this.parent).currentIndex]);
You're getting the error because it expects currentIndex to exist inside Nav.as, and it doesn't. It exists in the parent.
This is really a bit crufty of an approach I wouldn't want you to get used to. It's certainly very easy but the best approach is to allow classes to talk to each other if they need to. You mentioned objects do not exist past certain frames therefore I recommended this simple approach to keep a piece of data persistent through the whole timeline.
North America
Europe, Middle East and Africa
Asia Pacific