ActionScript Interaction
From MegaZine3
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.
Contents |
[edit] 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. This will give you a reference to the MegaZine instance the element resides in. There are two variants, one with only the engine reference, and one which also passes the PageSide the element resides on.
// Reference to the engine. function megazineSetup(megazine:IMegaZine):void; // Reference to engine and page side. function megazineSetup(megazine:IMegaZine, pageside:IPageSide):void;
This function is called by the engine as soon as the swf is done loading, passing the paramater so that the swf can work with it later on (or right away, of course). It normally makes sense to store the reference in some local variable. For more information on the received interface and functionality it provides, please refer to the API.
[edit] Referencing other elements
If you wish to communicate between a loaded swf and another element in the book there are three steps:
1. Enable the ElementIDs plugin.
<book plugins="elementids">
2. In the XML set the id attribute of the element that should be referenced, e.g. to myElement.
<img src="some.swf" id="myElement"/>
3. In the swf that should reference the element use the following code to get a reference:
// Assuming the variable holding the reference to the engine is named mz var plugin:IElementIds = mz.pluginManager.getPlugin("elementids"); if (plugin) { var proxy:IElementProxy = plugin.getElementById("myElement"); if (proxy.element) { // Element is loaded, work with it. var element:IElement = proxy.element; } else { // Element not loaded. } } else { // Plugin failed loading. }
If you have two swfs that should communicate, you can use the swf property of the img element (using its interface):
if (element is IImg) { var mySwf:DisplayObject = (element as IImg).swf; }
Then you can access everything in your swf using the mySwf variable.
[edit] Using events
To give an example on using events, here is how you would check when the page the swf resides on becomes visible:
var mz:IMegaZine; function megazineSetup(mz:IMegaZine, pageside:IPageSide):void { pageside.addEventListener(VisibilityEvent.VISIBLE, onPageSideVisible); } function onPageSideVisible(e:VisibilityEvent):void { // Page side is now visible. }
For more event types see the API.
[edit] 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 statement at the very top of your code:
import de.mightypirates.megazine.interfaces.*;
For the swf interaction you need the Image class:
import de.mightypirates.megazine.elements.Image;
And for the second example to work (using the event) you'll additionally need
import de.mightypirates.utils.events.VisibilityEvent;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.
