Skip navigation
Currently Being Moderated

How do I pass a variable between classes?

Sep 24, 2012 11:36 AM

Tags: #as3 #array #actionscript3 #variable_between #between_classes

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!

 
Replies
  • Currently Being Moderated
    Sep 24, 2012 11:55 AM   in reply to DanEdmonds

    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.

     
    |
    Mark as:
  • kglad
    63,053 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 24, 2012 11:54 AM   in reply to DanEdmonds

    your audioplayer class needs a reference to an instance of your nav class.

     

    if your nav class instance is added to the display list and your audioplayer is added to the display list, you don't need to pass a reference explicitly.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 24, 2012 12:59 PM   in reply to DanEdmonds

    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:

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fla sh/external/ExternalInterface.html

     

    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/

     
    |
    Mark as:
  • kglad
    63,053 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 24, 2012 1:35 PM   in reply to DanEdmonds

    in each file's constructor, use:

     

    this.addEventListener(Event.ADDED_TO_STAGE,addedF);

     

     

    and add the listener function to each class:

     

    private function addedF(e:Event):void{

    trace(this,e);

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 24, 2012 4:08 PM   in reply to DanEdmonds

    Are you importing them both as SWFs via Loaders or as code via import or .swc into your document?

     
    |
    Mark as:
  • kglad
    63,053 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 25, 2012 7:25 AM   in reply to DanEdmonds

    if you're trying to access index in AudioPlayer from nav, it must be made a public variable and you must use a nav reference in AudioPlayer.

     

    again, read message 6, follow the suggestion and report whether you see those two traces.

     
    |
    Mark as:
  • kglad
    63,053 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 25, 2012 9:51 AM   in reply to DanEdmonds

    is your nav instance on the main timeline?  is your audioplayer instance on the main timeline?

     

    (if you don't know, you can use trace(this.parent) in each class).

     
    |
    Mark as:
  • kglad
    63,053 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 25, 2012 4:04 PM   in reply to DanEdmonds

    then you'll need to use some other class that continues to exist when you the data are available in nav and when the data is needed in audioplayer.  or, if audioplayer can determine the needed data by checking your main timeline's current frame, it can use that.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2012 7:28 AM   in reply to DanEdmonds

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2012 8:36 AM   in reply to DanEdmonds

    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);

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2012 9:20 AM   in reply to DanEdmonds

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2012 9:49 AM   in reply to DanEdmonds

    You're welcome and good luck!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points