AS3 - 从类的根时间轴调用函数 [英] AS3 - Call function in root timeline from class

查看:136
本文介绍了AS3 - 从类的根时间轴调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要调用一个函数,它是闪存从外部类中的根时间轴内。

I want to call a function that is inside the root timeline of Flash from an external class.

这code是从类:

private function loadImage(event:Event):void
{
    addToContainer()   
}

在主时间轴:

function addToContainer():void
{
    trace("Called")
} 

如何管理?

推荐答案

你需要把addToContainer()在时间轴中?

Do you need to put addToContainer() in the timeline?

我会考虑从时间线上删除您code完全,而在它创建一个文档类与addToContainer代替。这使得它更容易跟踪你正在寻找什么。

I would consider removing your code entirely from the timeline, and creating a "Document class" with addToContainer in it instead. That makes it easier to keep track of what you're looking at.

public class FunctionTest extends MovieClip {
   protected static var _this:FunctionTest;
   function FunctionTest() { _this = this; }
   public static function get application_root():FunctionTest { return _this; }
   public function addToContainer():void { trace("Called"); }
}

现在你有写作的LoadImage两种方式。如果它是一个DisplayObject内(按照马蒂·华莱士的先前的评论),你可以这样说:

Now you have two ways of writing loadImage. If it's within a DisplayObject (as per Marty Wallace's earlier comment), you can say something like

(this.root as FunctionTest).addToContainer();

如果没有,你有,你可以在任何地方使用另一种:

If not, you have an alternative you can use from anywhere:

FunctionTest.application_root.addToContainer();

如果你真的要在时间线中定义addToContainer(),那么你就需要一个链接到显示根初始化外部类。做这样的事情:

If you really have to define addToContainer() in the timeline, then you will need to initialize the external class with a link to the display root. Do something like:

public class LoadImageClass {
   protected var _stored_root:MovieClip;
   function LoadImageClass(new_root:MovieClip) { this._stored_root = new_root; }
   public function loadImage():void {
      this._stored_root.addToContainer();
   }
}

这篇关于AS3 - 从类的根时间轴调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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