开发者

AS3: How do I add event listeners to objects in SWFs that I'm loading dynamically?

开发者 https://www.devze.com 2022-12-16 00:27 出处:网络
I have a parent SWF (Parent) that handles user navigation between multiple children SWFs (Child_1, Child_2, Child_3).

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.


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
 }
 ... 
}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号