Passing a variable between frames with actionscript 3

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 05:11:06

问题


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.


回答1:


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!




回答2:


frame scripting is, in my opinion, ghetto and should be avoided.

that being said, you problem could be solved a few ways. first, you create an "Actions" layer that is accessible to all frames by stretching the layer as long as your timeline, but all your actionscript is on the first frame. second, by placing your text field on a separate layer, accessible in frame one but only becomes visible in frame 4 (assuming you don't want it to appear until frame 4).

however, OOP and and a descent design pattern as Glycerine suggests is definatly the way to go, especially if you are new and plan on further investment in the flash platform.

some book suggestions: Colin Moock's Essential ActionScript 3.0, The ActionScript 3.0 Quick Reference Guide, Kieth Peter's Foundation Actionscript 3.0 Animation: Making Things Move! and AdvancED ActionScript 3.0 Animation. these books will certainly give you some solid ground in proper development for the flash platform. if you read them you wont regret it.




回答3:


Both answers are good (I upvoted on them), but judging from your code it looks like you are creating a simple form and perhaps don't feel like investing too much time atm. The quickest solution is to still code on the timeline, but place everything on the first frame. Instead of going to different frames you will be changing the visible property to hide and show different MovieClips.

So your dialog error box will always been there but hidden on startup, when the user enters in the data and I assume a submit button then your alert MovieClip can be shown. Because everything is in the first frame you can easily assign the values to the textfields.




回答4:


A simple fix would be creating global variable instead of local .

var myVar = "hi" ;

to

this.myVar = "hi" ;

If you decaled in Action Layer frame 1 , u can further access the this.myVar in other frames



来源:https://stackoverflow.com/questions/4028250/passing-a-variable-between-frames-with-actionscript-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!