Flash AS2 检测动态连接的组件已初始化

发布于 2024-11-17 02:14:15 字数 1095 浏览 4 评论 0原文

好吧,让我自己疯狂地试图记住 AS2,希望有更好的记忆力的人来帮忙......

基本上,我的问题是使用组合和“等待”组件 - 例如。一个按钮组件 - 准备附加一个点击处理程序..由于各种原因,我无法对整个设置做任何事情,所以情况如下...

我在库中有一个影片剪辑,没有类,只有一个链接 ID ( “附上我”)。该剪辑内部有一个 AS2 Button 组件(“btn”)。

我有一个类,它获取对时间线的引用,然后附加库中的 mc,然后向按钮添加事件侦听器。简单吧?

var foo:Foo = new Foo(this);

class Foo {

    private var tl:MovieClip; // timeline
    private var mc:MovieClip; // the attached movieclip

    function Foo(t:MovieClip){
        tl = t;
        mc = tl.attachMovie("AttachMe", "mc", 10);
        var b:Button = mc.btn;
        b.addEventListener("click", Delegate.create(this, onClick));
    }
    private function onClick(e:Object):Void{
        trace("Hi!");
    }
}

这是行不通的,因为 b.addEventListener 在我执行此操作时未定义...

所以说真的,这里的最佳实践是什么?即,这是一个真正的痛苦,我知道这是在添加事件处理程序之前“等待”按钮组件(在本例中)初始化的情况。

可以做一个间隔/超时——讨厌这个想法。可以在 mc 上执行 EnterFrame,在第一次调用时清除它,然后再次添加处理程序,似乎是错误的。 onLoad 不会为 movieclip 触发,因此无法添加它(如果我使用继承和库 movieclip 的自定义子类,我可以使用 onLoad)。

每一种方法似乎都是一种破解,一想到要重复多次就令人沮丧!多年来肯定已经死了,但我真的找不到任何直接提到问题和接受的解决方案......

任何想法表示赞赏!

富有的

OK, driving myself mad trying to remember AS2 here, hoping for someone with better memory to lend a hand...

Basically, my problem is using composition and 'waiting' for components - eg. a Button component - to be ready to attach a click handler..for various reasons I can't do anything about the whole setup so here's the situation...

I have a movieclip in the library, no class, just a linkage ID ("AttachMe"). That clip has an AS2 Button component inside it ('btn').

I have a class that gets a reference to a timeline, then attaches the mc from the library, then adds an event listener to the button. Simple right?

var foo:Foo = new Foo(this);

class Foo {

    private var tl:MovieClip; // timeline
    private var mc:MovieClip; // the attached movieclip

    function Foo(t:MovieClip){
        tl = t;
        mc = tl.attachMovie("AttachMe", "mc", 10);
        var b:Button = mc.btn;
        b.addEventListener("click", Delegate.create(this, onClick));
    }
    private function onClick(e:Object):Void{
        trace("Hi!");
    }
}

This won't work, as b.addEventListener is undefined at the point I'm doing it...

So really, what is best practice here? i.e. this is a real pain, and I know it's the case of 'waiting' for the button component (in this case) to initialise before adding the event handler.

Could do an interval/timeout - hate the idea of that. Could do enterFrame on the mc, clear it on first call then add the handler..again, just seems wrong.
onLoad doesn't fire for the movieclip so can't add that (if I used inheritance and a custom sub class for the library movieclip I could use onLoad).

Each way seems to be a hack and the thought of doing many times is depressing! It must have been done to death over the years, but I really can't find anything directly mentioning the problem and accepted solutions...

Any thoughts appreciated!

Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぶ宁プ宁ぶ 2024-11-24 02:14:15

是的,第三次尝试,我已将其他答案标记为删除。在进入 UIObject 和 Button 类之后,我想出了这个,这似乎有效:

import mx.utils.Delegate;
import mx.controls.Button;
import mx.events.EventDispatcher;

class Foo {

    private var tl:MovieClip; // timeline
    private var mc:MovieClip; // the attached movieclip

    function Foo(t:MovieClip)
    {
        tl = t;
        mc = tl.attachMovie("AttachMe", "mc", 10);
        var b:Button = mc.btn;
        EventDispatcher.initialize(b);
        b.addEventListener("click", Delegate.create(this, onClick));
    }
    private function onClick(e:Object):Void
    {
        trace("Hi!");
    }
}

这个重要的部分是 EventDispatcher.initialize(b); ,它似乎强制这些方法立即存在。

Right, third attempt, and I've marked the other answers for deletion. After getting into the UIObject and Button classes I came up with this, which seems to work:

import mx.utils.Delegate;
import mx.controls.Button;
import mx.events.EventDispatcher;

class Foo {

    private var tl:MovieClip; // timeline
    private var mc:MovieClip; // the attached movieclip

    function Foo(t:MovieClip)
    {
        tl = t;
        mc = tl.attachMovie("AttachMe", "mc", 10);
        var b:Button = mc.btn;
        EventDispatcher.initialize(b);
        b.addEventListener("click", Delegate.create(this, onClick));
    }
    private function onClick(e:Object):Void
    {
        trace("Hi!");
    }
}

This important bit is EventDispatcher.initialize(b); which seems to force the methods into existence straight away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文