Passing a variable between frames with actionscript 3

Jennifer picture Jennifer · Oct 26, 2010 · Viewed 16.4k times · Source

I am new to actionscript 3.0 and I'm experiencing difficulty trying to pass a variable that is created and set in frame 1 to a dynamic text box that is added to the stage in frame 4.

on frame 1 the variable is set from information entered by the user: var input_dia = ""; input_dia = pdia_input.text;

and should be displayed in a dynamic text box on frame 4: dia_alert.text=input_dia;

I'm receiving the following error: 1120: Access of undefined property input_dia.

Answer

Glycerine picture Glycerine · Oct 27, 2010

You have to imagine - the different scenes are a lot like separate movies and flash has trouble sharing between them.

The correct way to do it - is begin to use the OOP (object orientated programming) of AS3. You will need to create something called a document class. This is effectively code that lives forever behind the scenes (no pun intended). You can store stuff in this class and read them later on whenever you please.

Its easier than it sounds and once set up - it'll allow you to start moving the code off your timeline.


First create a file called "DocumentClass.as" This can really be called anything, but calling it this is very good practice.

Save this file in the same place as the FLA you're working with - the same folder.


In CS3 - In the properties panel at the bottom of your screen - when you have the stage selected there will be a little box allowing you to type the name of the document class into it. Type the name of the file you just made 'DocumentClass' *without the '.as' extension - click on the link if you're unsure where it is you need to type.

http://curtismorley.com/wp-content/uploads/2007/07/documentclasspath_bad.JPG

Please note the capitlization - this is good practice


Open this file in Flash and write the following code. Exactly as I write it

DocumentClass.as

package {

  //Call this class the SAME NAME as the file - VERY IMPORTANT
  class DocumentClass extends MovieClip
  {

    //This is an example of a variable - a container
    //of information of which is public - and can be 
    //seen by all the scenes in your flash movie.
    public var myName:String = "Jay Jagpal";

    //This is called a construct - this function automatically
    //runs when this class is seen by flash.
    public function DocumentClass()
    {
      //nothing needs to go here for you today...
    }
  }

}

You can see inside all the guff I write for you I have a variable called myName - You can create what you want - myAge... textToBeInAllScenes... girlfriendsWeightToday... Call then anything.


Further Explination

A class is a block of code that is created in memory when needed. A DocumentClass is this - but lives all the way through the life of your application.

A package - is just fancy as3 speak for 'put this stuff in a box' - it can get more advanced but thats the jist.

class DocumentClass extends MovieClip - You're telling flash "my class is called DocumentClass" - this extends something called a MovieClip.

The MovieClip is a class, exactly like yours - but made for you and lives inside flash. This contains lots of code to make animations work. Your Flash scene itself is just a visible version of this MovieClip thing.

you have to extend this class, because you pretty much want to [in a fake way] copy paste all the finished code and use it in your DocumentClass. You're now extending MovieClip and by doing so, your code is piled on top of already existing stuff.

public function DocumentClass() - yes this is a function. But its called a 'construct'. Its a special type of function that lives inside a class. Firstly it has the same name. This allows Flash to find it really easily. Its special job is to instantly start running its code automatically when this class is made and seen in flash. All automatic see...


The important part for you is the public var I added. This is a bucket you can store your information in.

The public part tells flash, anything can see it if they want to, scenes, other classes... people in the street - Anything!

The :string after your variable (or bucket) name, it telling flash what type of information will be stored inside the var. This isn't app breaking important - but for good OOP code - do it. (google AS3 variable casting)

There are many var types, String, Number, int, Boolean and so one... roughly about 7 basic.


I think thats enough of an answer for StackOverflow - it will work -

be warned Most errors are your spelling errors... Flash don't like spelling errors.


Enjoy!