AS3: How to dispatch function from class to mainframe

34 views Asked by At

This might be a silly question. But how can I call a function (to execute) from a class to the timeline.

For example, I have the class "Test" and I want to execute the function "Next" on the timline (which it is only a function to show next slide).

Hope you understand what I'm trying to do.

Thank you!

1

There are 1 answers

2
null On BEST ANSWER

The best practice for communication (in this scenario!) is to use Events.

  1. The timeline create the object of your class Test and registers an event listener.
  2. The object of your class Test dispatch an Event.
  3. The function that the timeline registered for that Event will be executed.

Please take a look at this question that wants to send additional information to the main timeline. In your case, you do not need a custom Event, because you do not want to send any information along. You only want to communicate the occurrence of the event. You can put that information into the type of the event. an example for a dispatch could look like this:

dispatchEvent(new Event("next"));

Creating a custom class allows you to put that String literal that describes the type into a constant, which prevents errors caused by accidentally misspelling the type. That might be a reason to create a custom Event class anyway, even only for the sake of a place to put those constants.

dispatchEvent(new PresentationEvent(PresentationEvent.NEXT));

Again, this would do the same as the previous line. this is also covered in the other question and the answer to it. Please take a look.