在AS3全局变量 [英] global variables in AS3

查看:257
本文介绍了在AS3全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我希望能够在由Flash文件加载的Flash文件或Flash文件从任何地方访问某些变量。我该怎么办呢?我不知道是什么类的,我不想学什么类,我不希望导入什么,我只是希望能够初始化和从任何地方访问某些变量。

Ok, I want to be able to access certain variables from anywhere within a Flash file or Flash files loaded by that Flash file. How do I do it? I don't know what classes are, I don't want to learn what classes are, I don't want to import anything, I just want to be able to initialize and access certain variables from anywhere.

感谢:)

推荐答案

那么,有没有更多的_global像有在AS2 - 既然你不想使用类不能使用静态变量(我如果你有兴趣解释这些)。所以,留给你的使用根变量。

Well, there is no more _global like there was in as2 - and since you don't want to use classes you can't use static variables (I can explain these if you're interested). So you're left with using variables on the root. For example, you can define a variable on the main timeline like this:

var myGlobal:Number = 100;

如果您要访问其他地方?就是其他影片剪辑的时间轴上,你需要说的:

If you want to access it elsewhere ... that is, on the timeline of other movieClips you need to say:

MovieClip(root).myGlobal;

这意味着,如果你从来没有见过的可能看起来荒谬的。基本上,我们是铸造根到MovieClip给我们访问它的动态特性。幸运的是,你可以设定,让你不必继续写作影片剪辑(根)所有的时间:

Which if you've never seen before probably looks absurd. Basically we are casting the root to a movieClip to give us access to its dynamic properties. Luckily, you can set it up so you don't have to keep writing MovieClip(root) all the time:

// do this on any movieClip where you want to access globals
var global:MovieClip = MovieClip(root);

trace(global.myGlobal);

所以,最后的code它只是一个额外的行,以获得功能恢复的方式,它是在AS2。

So in the end its just one extra line of code to get the functionality back to the way it was in AS2.

修改

  1. 进入主时间轴,并添加到您的行为:VAR myGlobal:数= 100;
  2. 请新的MovieClip确保其在舞台上
  3. 进入新的MovieClip,并添加到您的行动:

  1. go to main timeline and add this to your actions: var myGlobal:Number = 100;
  2. make a new movieClip make sure its on the stage
  3. go into the new movieClip and add this to your actions :

VAR全球:影片剪辑=影片剪辑(根); 跟踪(global.myGlobal);

var global:MovieClip = MovieClip(root); trace(global.myGlobal);

测试你的影片

这篇关于在AS3全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆