Author Topic: [Tutorial] ActionScript Interaction (EN)  (Read 8605 times)

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1985
  • MegaZine3 Developer
    • MegaZine3
[Tutorial] ActionScript Interaction (EN)
« on: January 30, 2009, 09:32:23 pm »
ActionScript Interaction

If you load your own swf files into the engine you might want to interact with it, to allow for more specialized behavior - e.g. an animation starting when the page the clip resides on becomes visible. Here is how to connect your code to the engine.


Setup / Connecting

To get a reference to the engine, all you have to do is add a function to the root of your swf, named megazineSetup. It comes in multiple flavors, so pick the one you need:

Code: (actionscript3)
  1. // This will pass a reference to the instance of the engine,
  2. // the page the swf resides in, if it's on the odd or even page,
  3. // and a reference to the graphics library used for building the gui.
  4. megazineSetup(megazine:IMegaZine, page:IPage, even:Boolean, library:ILibrary)
  5.  
  6. // Like the first, but no reference to the library.
  7. megazineSetup(megazine:IMegaZine, page:IPage, even:Boolean)
  8.  
  9. // Only a reference to the engine itself.
  10. megazineSetup(megazine:IMegaZine)

This function is called by the engines as soon as the swf is ready, passing the paramaters so that the swf can work with those later on. Therefore it normally makes sense to store the parameters in some local variables.
For more information on the classes (IMegaZine, IPage, ILibrary) please refer to the API.


Referencing other elements

If you wish to communicate between a loaded swf and another element in the book there are two steps:
1) In the XML set the id attribute of the element that should be referenced, e.g. myElement.

Code: (xml)
  1. <img src="some.swf" id="myElement"/>

2) In the swf that should reference the element use the following code to get a reference:

Code: (actionscript3)
  1. // Assuming the variable holding the reference to the engine is named 'mz'
  2. var element:AbstractElement = mz.getElementById("myElement").element;

If you have two swfs that should communicate:

Code: (actionscript3)
  1. // Continuing from above
  2. var swf:DisplayObject = element.swf; // For this to work the element must be of class Image

Then you can access everything in your swf using the swf variable.


Using events

To give an example on using events, here is how you would check when the page the swf resides on becomes visible:

Code: (actionscript3)
  1. var mz:IMegaZine;
  2. function megazineSetup(mz:IMegaZine, page:IPage, even:Boolean):void {
  3.    page.addEventListener(even ? MegaZineEvent.VISIBLE_EVEN : MegaZineEvent.VISIBLE_ODD, onPageVisible);
  4. }
  5.  
  6. function onPageVisible(e:MegaZineEvent):void {
  7.    // Page is now visible
  8. }

For more event types see the API, namely for IMegaZine and IPage.


Sources

To compile your code when using the interfaces/classes, make sure to have the sources that come with the releases placed in your include path, and you'll need to import them by adding the following statements at the very top of your code:

Code: (actionscript3)
  1. import de.mightypirates.megazine.IMegaZine;
  2. import de.mightypirates.megazine.IPage;
  3. import de.mightypirates.megazine.elements.AbstractElement;
  4. import de.mightypirates.megazine.gui.ILibrary;

And for the example to work (using the event) you'll additionally need
Code: (actionscript3)
  1. import de.mightypirates.megazine.events.MegaZineEvent;

Alternatively, if you don't care for the types you can just declare all of the variables of type Object (or *), of course. Using the classes is much recommended, though, otherwise typos or wrong assumptions can be a real pain.
« Last Edit: May 18, 2009, 03:10:11 pm by Florian Nücke »
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.