use global item in as3

[ 2009-06-03 00:11:52 | Author: liuhuan ]
Font Size: Large | Medium | Small
It's very fast for us to get to a certain object directly by _global or _root in as2. However, we can't simply use _global in as3, as there's no more _global.

For example, a.swf loads b.swf, if all of the code is in "as2 style" which spread roughly in timeline, how can we get a certain object in a.swf directly?

Of cource we can use path in b.swf such as: this.parent.parent....parent.mc. But in strict mode, it's not allowed as there's no such mc in the path. We can turn off the strict mode, which does not make sense. Moreover, the long path is hard to read and maintain.

How can we get back the old "_global" in as3? A simple way is to create a class defining static variables. Like:
package {
  public final class GlobalItems {
    public static var _musicplayer:*
  }
}

save as GlobalItems.as in the same folder with a.fla and b.fla.

So now, either a.swf or b.swf we can easily get this global item.

in a.swf:

GlobalItems._musicplayer = mMusicPlayer

in b.swf, we can just simply use "GlobalItems._musicplayer" to get the mMusicPlayer object in a.swf.
Comments Feed Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=984

There is no comment on this article.

You can't post comment on this article.