AS3:如何将事件侦听器添加到动态加载的 SWF 中的对象?

发布于 2024-08-17 10:36:59 字数 526 浏览 9 评论 0原文

我有一个父 SWF(父级),用于处理多个子 SWF(Child_1、Child_2、Child_3)之间的用户导航。

如何将事件侦听器添加到子 SWF 上的导航按钮,以便用户可以在 Child_1、Child_2 和 Child_3 之间横向移动?

我可以想到两个选项,但无法让其中任何一个起作用:

1) 父 SWF 在加载子 SWF 时设置事件侦听器。因此,我在 Parent 上使用加载程序来加载 Child_1,并在 Parent 中将事件侦听器添加到 Child_1.myNavigationform.myButton。

这里的问题是,Parent 只处理加载程序实例,我无法想象它如何深入到 Child_1 中的各个对象。

2) Child_1 将事件监听器添加到它自己的对象中。这正好扭转了问题。 Child_1 访问自己的按钮没有问题,但当用户单击它们时,它无法访问父级上的方法,以便加载 Child_2。

这里有什么解决办法呢?我确信这种情况经常发生,我只是没有看到任何解释如何处理这个问题的内容。

谢谢。

I have a parent SWF (Parent) that handles user navigation between multiple children SWFs (Child_1, Child_2, Child_3).

How do I add event listeners to the navigation buttons on the children SWFs so that the user can move laterally between Child_1, Child_2 and Child_3?

I can think of two options but can't get either one to work:

1) The parent SWF sets up the event listeners when it loads a child. So, I use a loader on Parent to load Child_1 and in Parent add eventlisteners to Child_1.myNavigationform.myButton.

The problem here is that the Parent is only handing the loader instance and I can't think of how it would drill down to the individual objects within Child_1.

2) Child_1 adds the event listeners to its own objects. This just reverses the problem. Child_1 has no problem accessing its own buttons but when a user clicks them it has no way of accessing methods on parent so that Child_2 can be loaded.

What's the solution here? I'm sure this is done frequently I just haven't seen anything out there that explains how to handle this.

Thank you.

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

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

发布评论

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

评论(1

鹿港巷口少年归 2024-08-24 10:36:59

你可以和他们两个一起去。但第一个更灵活,因为它提供松散耦合的父母和孩子电影。您可以通过 contentloaderinfo 获取子影片剪辑。

_child1loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_child1loader.load(child1URLRequest);
function onComplete(e:Event):void {
  _child1movie = MovieClip(e.target.content);
}

Now you can add this to your parent movie and call its functions, add listener's to its components.
In second case, if you want to access parent movie functionality then you need to put that thing inside your child's movies. (e.g. parent.somefunction();).
And much better implementation would be in which each child movie is giving some functionality via dispatching events (i.e. navigateNextEvent etc which can be used by loader movies i.e. in this case parent movie). So in that case implementation would go like.

public class Child1 extends MovieClip {
  ...
  private function onSomeButtonPressWhichWillAskParentToGoToNextChild():void {
    dispatchEvent(new Event("navigateNext"));
  }
  ...
}
...
public class Parent extends MovieClip{
 ...
 _child1movie.addEventListener("navigateNext", onNavigateNext);
 private function onNavigateNext(e:Event):void {
 //e.target is _child1movie, so you can play with it (hide it or throw it away etc...)
 //do actual naigation
 }
 ... 
}

You can go with both of them. But first one is more flexible as it provides loosely coupled parent and child movies. you can get the child movieclip via the contentloaderinfo.

_child1loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_child1loader.load(child1URLRequest);
function onComplete(e:Event):void {
  _child1movie = MovieClip(e.target.content);
}

Now you can add this to your parent movie and call its functions, add listener's to its components.
In second case, if you want to access parent movie functionality then you need to put that thing inside your child's movies. (e.g. parent.somefunction();).
And much better implementation would be in which each child movie is giving some functionality via dispatching events (i.e. navigateNextEvent etc which can be used by loader movies i.e. in this case parent movie). So in that case implementation would go like.

public class Child1 extends MovieClip {
  ...
  private function onSomeButtonPressWhichWillAskParentToGoToNextChild():void {
    dispatchEvent(new Event("navigateNext"));
  }
  ...
}
...
public class Parent extends MovieClip{
 ...
 _child1movie.addEventListener("navigateNext", onNavigateNext);
 private function onNavigateNext(e:Event):void {
 //e.target is _child1movie, so you can play with it (hide it or throw it away etc...)
 //do actual naigation
 }
 ... 
}

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