Dispatching Events from an animation
Often an animation is not just a set of frames that needs to be played back. Sometimes an animation should trigger some actions inside of your application. The moment to start the action also needs to be configured within the animation. Flash-developers can use Events inside flash-animations and handle them in a flash application using Action Script 3. This easy-to-use approach simplifies development and makes complex things simple. GAF allows other developers use the same approach in their applications on other frameworks/technologies.
How to prepare an animation in Flash Pro
Open FLA file with an animation and find a Movie Clip where you want to insert a trigger. You can insert a trigger into any Movie Clip in any frame. The trigger can also be inserted inside nested Movie Clips at any depth of nesting. After you found an appropriate frame, do the following:
- Create new layer in the Movie Clip and call it "Actions" (you can use any existing layer with any name, but we suggest you create a new one to make your workflow with Events more comfortable);
- Find a frame where you want to insert a trigger (for example frame number 100);
- Select the frame 100 on layer "Actions" and make it a Keyframe (press the F6 button on keyboard or right click the frame and select "Insert Keyframe" from context menu);
- Open Actions panel (press the F9 button on keyboard or go to Main Menu → Window → Actions) and insert the following code:
- Done. Now your animation will dispatch the event with the name "EventName" when playhead reaches the frame with the corresponding event inserted.
You can use any name for the event. Just change "EventName" to any appropriate name, that easy.
You can also dispatch an event with additional data (if you need to). Additional data is represented as a String and can contain any information you need. Insert the following code in Actions panel to dispatch an event with additional data:
How to work with Events in runtime
Cocos2D-X v3 GAF
You can find an example of how to work with Events in Cocos2D-X GAF library sample in the 'tests' directory. Check 'custom_event.fla' and 'EventsTest' class
Example for this library is under development and will be available in the near future.