Reputation: 27845
I want to create a custom event and it will be accessible by any other class.
Suppose I have NavigationMenuClass.as
which contains some next, prev buttons
, and another class For Page.as
which should show a particular page at each next
or prev
button press.
And I need to create a custom event(will write a EVENTClass.as to manage all these kind of events.) called "showPage"
, and when this event occures the Page.as class member function need be called.
private function nextPress(event:Event) {
//dispatchEvent(new Event("showPage"));
// this will call the Page class Menmber function page:Page = new Page; page.showNextPage();
}
With out passing the object how can I call a particular member function using Event
and Event Dispatcher
methods.
Upvotes: 0
Views: 2676
Reputation: 39408
You seem kind of confused.
So, you have a display hierarchy in which your Page.as display object is the parent of the NavigationMenuClass.as, correct?
Your NavigationMenuClass should dispatch events for 'next' and 'previous'. The Page class should listen to those events and do something.
Your code kind of already does the dispatching part, although it is commented out. I would put this in the click handler of your button and use it to dispatch a next, or previous, event:
private function nextPress(event:Event) {
// dispatchEvent(new Event("showPage"));
// this will call the Page class Menmber function page:Page = new Page; page.showNextPage();
dispatchEvent(new Event("next"));
}
In your Page.as you just add an event listener:
navigationMenuClassInstance.addEventListener('next', onNext);
public function onNext(event:Event):void{
// write code to change content
}
You can use the same approach to implement the previous event. To address a few of your "odd wordings"
I want to create a custom event and it will be accessible by any other class
I'm not sure what you mean by accessible.
You can create a custom event class that extends Event and any other class can use it, as long as it imports it. My example above doesn't create a custom event class, but uses the default event class.
In terms of dispatching, the event only dispatches itself up to its' parent. IF the event bubbles, it will go to it's parent parent, then the parent's parent, and so on all the way up the the stage.
With out passing the object how can I call a particular member function using Event and Event Dispatcher methods.
I'm not sure what you mean by "passing the object" here. When you dispatch an event, that event class is always available to the event listener as an argument to that event listener method. That is kind of like passing an object.
Upvotes: 5
Reputation: 439
Also when following Flextras excellent guide, make sure that NavigationMenuClass extends EventDispatcher, otherwise just calling the dispatchEvent() method won't work!
Upvotes: 1