Passing a variable between frames using ActionScript 3

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

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

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

I get the following error: 1120: access to input_dia property.

+4
source share
4 answers

You have to imagine that different scenes are very similar to individual films, and the flash has problems with sharing.

The right way to do this is to start using OOP (Object Oriented Programming) AS3. You will need to create something called a document class. This is an efficient code that lives forever behind the scenes (no pun intended). You can store things in this class and read them later when you want.

Its easier than it seems, and after setting it up - this will allow you to start moving the code from your timeline.


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

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


In CS3 - in the properties panel at the bottom of the screen - when you select a scene, a small field will appear allowing you to enter the document class name in it. Enter the name of the file you just made "DocumentClass" * without the .as extension. - click the link if you don’t know where exactly you need to enter.

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

Pay attention to capitlization is good practice


Open this file in Flash and write the following code. Exactly how 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 that I write for you. I have a variable called myName. You can create what you want. myAge ... textToBeInAllScenes ... girlfriendsWeightToday ... Call then something.


Further explanation

A class is a block of code that is created in memory when necessary. DocumentClass is that, but it is completely up to your application.

The package - just freaky as3 says it "put it in a box" - it may become more advanced, but that is jist.

the DocumentClass class extends MovieClip - you say flash "my class is called DocumentClass" - this extends something called MovieClip.

MovieClip is a class exactly the same as yours, but made for you and residing inside flash memory. This contains a lot of code for the animation to work. Your Flash scene itself is just the visible version of this MovieClip thing.

you need to extend this class because you pretty much want to [in fake] copy paste all the finished code and use it in your DocumentClass. You are now expanding MovieClip and so your code is stacked on top of existing material.

public function DocumentClass () - yes, this is a function. But it is called "construction." This is a special type of function that lives inside a class. Firstly, it has the same name. This allows Flash to easily find it. Its special task is to instantly run its code automatically when this class is created and seen in a flash. All automatic see ....


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

The public part reports flash, everyone can see it, if they want, scenes, other classes ... people on the street - nothing!

String: after the name of the variable (or bucket), which indicates what information will be stored inside var. This is not an application that matters, but for good OOP code - do it. (listing of Google variable AS3)

There are many types var, String, Number, int, Boolean, etc. about 7 major.


I think the answer is enough for StackOverflow - it will work -

Most errors will be warned - your spelling mistakes ... Flash doesn't like spelling mistakes.


Enjoy it!

+11
source

Frame scripts are, in my opinion, ghettos and should be avoided.

saying that the problem can be solved in several ways. firstly, you create an β€œActions” layer, accessible to all frames, stretching the layer until your timeline, but all of your ActionScript is in the first frame. secondly, placing your text field on a separate layer, available in frame one, but only becomes visible in frame 4 (if you do not want it to be displayed before frame 4).

however, OOP and the descent design structure, as Glycerin suggests, are definitely a way out, especially if you are a beginner and are planning further investments in a flash platform.

some book suggestions: Colin Moock Essential ActionScript 3.0 , ActionScript 3.0 Quick Reference Guide , Kieth Peter Foundation ActionScript 3.0 Animation: Creating Things Move! and AdvancED ActionScript 3.0 Animation . these books will surely give you a solid foundation for properly developing your flash platform. if you read them, you will not regret it.

+1
source

Both answers are good (I support them), but judging by your code, it looks like you are creating a simple form and maybe you don't want to invest too much time. The quickest solution is to still encode on the timeline, but put everything in the first frame. Instead of going into different frames, you will change the visible property to hide and show different clicks.

So your error dialog was always there, but hidden at startup, when the user enters data, and I assume the submit button, then your MovieClip warning can be shown. Since everything is in the first frame, you can easily assign values ​​to text fields.

+1
source

A simple fix will create a global variable instead of a local one.

var myVar = "hi" ;

to

this.myVar = "hi" ;

If you are decalized in an action level 1 frame, you can access this.myVar in other frames

0
source

All Articles