AS3 中的全局变量 [英] global variables in AS3

查看:73
本文介绍了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.

谢谢:)

推荐答案

好吧,没有像 as2 那样的 _global - 而且由于您不想使用类,所以不能使用静态变量(我可以如果您有兴趣,请解释这些).所以你只能在根上使用变量.例如,您可以像这样在主时间线上定义一个变量:

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;

如果你想在别处访问它......也就是在其他movieClips的时间线上你需要说:

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

MovieClip(root).myGlobal;

如果您以前从未见过,这可能看起来很荒谬.基本上,我们将根转换为一个影片剪辑,以便我们访问其动态属性.幸运的是,您可以进行设置,这样您就不必一直编写 MovieClip(root):

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);

所以最后只需要多一行代码就可以将功能恢复到 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:Number = 100;
  2. 制作一个新的电影剪辑,确保它在舞台上
  3. 进入新的影片剪辑并将其添加到您的动作中:

  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:MovieClip = MovieClip(root);跟踪(global.myGlobal);

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

测试你的电影

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

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