使用 actionscript 3 在帧之间传递变量
我是 Actionscript 3.0 的新手,在尝试将在第 1 帧中创建和设置的变量传递到添加到第 4 帧中的舞台的动态文本框时遇到困难。
在第 1 帧上,变量是根据输入的信息设置的由用户: var input_dia = ""; input_dia = pdia_input.text;
并且应该显示在第 4 帧的动态文本框中: dia_alert.text=input_dia;
我收到以下错误: 1120: 访问未定义的属性 input_dia。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你必须想象 - 不同的场景很像单独的电影,而 Flash 很难在它们之间共享。
正确的方法是开始使用AS3的OOP(面向对象编程)。
您将需要创建称为文档类的东西。这是有效的代码,永远存在于幕后(没有双关语的意思)。您可以将内容存储在此类中,并在以后随时阅读。
它比听起来更容易,一旦设置完毕 - 它将允许您开始将代码移出时间线。
首先创建一个名为“DocumentClass.as”的文件,这实际上可以被称为任何名称,但这样称呼它是非常好的做法。
将此文件保存在与您正在使用的 FLA 相同的位置 - 同一文件夹。
在 CS3 中 - 在屏幕底部的属性面板中 - 当您选择了阶段时,将会出现一个小框,允许您在其中键入文档类的名称。输入您刚刚创建的文件名“DocumentClass”*,不带“.as”扩展名 - 如果您不确定需要在哪里输入,请单击链接。
http://curtismorley.com/wp-content/uploads/2007/07 /documentclasspath_bad.JPG
请注意大小写 - 这是一个很好的做法
在 Flash 中打开此文件并编写以下代码。正如我所写的
DocumentClass.as,
您可以在我为您编写的所有内容中看到我有一个名为 myName 的变量 - 您可以创建您想要的内容 - myAge...textToBeInAllScenes...girlfriendsWeightToday。 .. 打电话然后就可以了。
进一步说明
类是需要时在内存中创建的代码块。 DocumentClass 就是这样的 - 但它始终存在于应用程序的生命周期中。
一个包 - 只是很奇特的as3,意思是“把这些东西放在一个盒子里” - 它可以变得更先进,但这就是要点。
class DocumentClass extends MovieClip - 您告诉 flash“我的类称为 DocumentClass” - 这扩展了称为 MovieClip 的东西。
MovieClip 是一个类,与您的类完全一样 - 但为您设计并存在于 flash 中。其中包含大量使动画正常工作的代码。您的 Flash 场景本身只是此 MovieClip 事物的可见版本。
你必须扩展这个类,因为你非常想[以一种假的方式]复制粘贴所有完成的代码并在你的DocumentClass中使用它。您现在正在扩展 MovieClip,通过这样做,您的代码将堆积在现有的内容之上。
public function DocumentClass() - 是的,这是一个函数。但它被称为“构造”。它是一种特殊类型的函数,存在于类中。首先它有相同的名字。这使得 Flash 能够很容易地找到它。它的特殊工作是当此类在 Flash 中创建并可见时立即开始自动运行其代码。所有自动查看...
对您来说重要的部分是我添加的
public var
。这是一个可以存储信息的存储桶。public 部分告诉 flash,任何人都可以看到它(如果他们愿意)、场景、其他类...街上的人 - 任何东西!
变量(或存储桶)名称后面的 :string 告诉 flash 变量中将存储什么类型的信息。这对应用程序破坏并不重要——但是为了好的 OOP 代码——就这样做。 (google AS3变量转换)
有很多var类型,String,Number,int,Boolean等等...大约7个基本的。
我认为这对于 StackOverflow 来说已经足够了 - 它会起作用 -
被警告 大多数错误都是你的拼写错误... Flash 不喜欢拼写错误。
享受!
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
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!
在我看来,框架脚本是贫民窟,应该避免。
话虽如此,您的问题可以通过几种方式解决。首先,您创建一个“动作”图层,通过将图层拉伸到与时间轴一样长,所有帧都可以访问该图层,但所有动作脚本都位于第一帧上。其次,通过将文本字段放置在单独的图层上,可以在第一帧中访问,但仅在第 4 帧中可见(假设您不希望它在第 4 帧之前出现)。
然而,Glycerine 所建议的 OOP 和血统设计模式绝对是正确的选择,特别是如果您是新手并计划进一步投资闪存平台的话。
一些书籍建议:Colin Moock 的 Essential ActionScript 3.0、ActionScript 3.0 快速参考指南,Kieth Peter's Foundation Actionscript 3.0 动画:让事物动起来! 和 高级 ActionScript 3.0 动画。这些书籍肯定会为您正确开发 Flash 平台奠定坚实的基础。如果你读过它们,你不会后悔的。
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.
这两个答案都很好(我对它们投了赞成票),但是从您的代码来看,您似乎正在创建一个简单的表单,并且可能不想投入太多时间。最快的解决方案是仍然在时间轴上编码,但将所有内容放在第一帧上。您将更改可见属性以隐藏和显示不同的影片剪辑,而不是转到不同的帧。
因此,您的对话框错误框将始终存在,但在启动时隐藏,当用户输入数据并且我假设有一个提交按钮时,可以显示您的警报 MovieClip。因为所有内容都在第一帧中,所以您可以轻松地将值分配给文本字段。
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.
一个简单的修复方法是创建全局变量而不是局部变量。
var myVar = "hi" ;
到
this.myVar = "hi" ;
如果您在操作层第 1 帧中贴花,您可以进一步访问 this.myVar 在其他框架中
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