Flash AS2 检测动态连接的组件已初始化
好吧,让我自己疯狂地试图记住 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,第三次尝试,我已将其他答案标记为删除。在进入 UIObject 和 Button 类之后,我想出了这个,这似乎有效:
这个重要的部分是 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:
This important bit is
EventDispatcher.initialize(b);
which seems to force the methods into existence straight away.