Code Snippets
This page contains some code snippets, be they ActionScript3 or JavaScript, or whatever other language. Also check the forum for other code snippets.
Contents |
[edit] ActionScript3
[edit] Starting SWFs when the containing page becomes visible
This is a common problem: you load a SWF file into a page and want an animation to start as soon as its containing page becomes the main page / visible.
The basic skeleton code that goes into your SWF looks like this:
// Imports. Either get the source from SVN or use the SWC file (downloads section of the homepage) // Alternatively, replace all engine specific class occurrences (i.e. IMegaZine, etc) with either "*" or "Object". import de.mightypirates.megazine.interfaces.*; // Stop the SWF initially. stop(); // Called by the engine when the SWF finishes loading. function megazineSetup(megazine:IMegaZine, pageside:IPageSide):void { }
[edit] Start when visible
To start the SWF when the containing page side becomes visible:
import de.mightypirates.megazine.interfaces.*; import de.mightypirates.utils.events.VisibilityEvent; //< additionally. stop(); function megazineSetup(megazine:IMegaZine, pageside:IPageSide):void { pageside.addEventListener(VisibilityEvent.VISIBLE, handleVisible, //< event string is "visible" false, 0, true); //< avoid memory leaks // Remove if unnecessary. pageside.addEventListener(VisibilityEvent.INVISIBLE, handleInvisible, //< event string is "invisible" false, 0, true); //< avoid memory leaks // Initial check if page is already visible if (pageside.visible) { handleVisible(null); } else { handleInvisible(null); } } function handleVisible(e:Event):void { // Now visible. play(); } // Remove if unnecessary. function handleInvisible(e:Event):void { // Now invisible. stop(); }
[edit] Start when main page
To start the SWF when the containing page side becomes the main page (i.e. it is not only visible, but the current "main" page, i.e. the page visible if no page is dragged):
import de.mightypirates.megazine.interfaces.*; import de.mightypirates.megazine.events.*; //< additionally. stop(); var pageNumber:int; function megazineSetup(megazine:IMegaZine, pageside:IPageSide):void { pageNumber = pageside.number + (pageside.number & 1); // get even number of the double page this page side belongs to megazine.addEventListener(PageChangeEvent.PAGE_CHANGE, handlePageChange, //< event string is "page_change" false, 0, true); //< avoid memory leaks } function handlePageChange(e:PageChangeEvent):void { if (e.newPage == pageNumber) { // Now main page play(); } else if (e.oldPage == pageNumber) { // No longer main page (remove this if unneeded). stop(); } }
[edit] Checking if a page is loaded
To check if a page is loaded, there are two possibilities: use the IPageSide's property state, or register for StateChangeEvents on a page side. Here is an example that does both:
import de.mightypirates.megazine.interfaces.*; import de.mightypirates.utils.events.*; var pageside:IPageSide = ...; // e.g. via megazine.getPageSide() if (pageside.state == Constants.PAGE_SIDE_STATE_LOADED) { // loaded, do stuff doStuffForPageside(pageside); } else { // not loaded, add listener and wait. pageside.addEventListener(StateChangeEvent.STATE_CHANGE, handleStateChange, false, 0, true); } function handleStateChange(e:StateChangeEvent):void { if (e.newState == Constants.PAGE_SIDE_STATE_LOADED) { // now loaded var pageside:IPageSide = IPageSide(e.currentTarget); // or e.target, I never know... pageside.removeEventListener(StateChangeEvent.STATE_CHANGE, handleStateChange); doStuffForPageside(pageside); } } function doStuffForPageside(pageside:IPageSide):void { // Perform actions on a page side that is guaranteed to be loaded. }
[edit] Creating a book from code
To create a book just using code, not based off of an XML file, you can use the following approach. There are two ways of going about that, one being to basically dynamically generating the XML, the other being to use actual data you already have loaded. Note that for the second case loading and unloading does not work, so all data will always be present in memory.
import de.mightypirates.megazine.MegaZine; import de.mightypirates.megazine.interfaces.*; /* Create instance and add to stage. */ var mz:MegaZine = new MegaZine(); addChild(mz); /* Add listener to wait for book to be ready. */ mz.addEventListener(Event.COMPLETE, handleMegaZineComplete); /* Init book. */ mz.loadXML(); /* Note: you can change the settings used by the book using the third parameter, e.g. */ //mz.loadXML(null, null, {pagewidth: 400, pageheight: 600, plugins: "navigationbar"}); /* Note 2: when using multiple books on the same domain, you should also provide the * fourth parameter, which should be a unique identifier for a book. This is used for * letting certain plugins, e.g. the bookmarks plugin, know for which book to restore * locally stored information (in the bookmarks example, user set bookmarks). */ //mz.loadXML(null, null, null, "somebook"); /* Engine finished initialization, now it's OK to interact with it. */ function handleMegaZineComplete(e:Event):void { /* Method 1, generating content dynamically. You can dynamically add content using * either addChapter or addPage. For example */ mz.addPage(); mz.addPage(); mz.addPage(); mz.addPage(); /* adds 8 page sides (4 'physical' pages) to the book. The first call also causes a * new chapter to be created. You will probably want to pass page content: */ mz.addPage(<page><img src="../img.jpg"/></page> + <page><vid src="vid.flv"/></page>); /* Method 2, adding 'existing' content. You first have to add the pages you need, so * this is the same as above. But this time, store a reference to the generated page. */ var page:IPage = mz.addPage(); /* Now we can access that page's sides via page.even and page.odd. Those getters return * an interface, so we'll want to cast it to something more suited for our needs: */ var side:DisplayObjectContainer = DisplayObjectContainer(page.even); /* Now we can add stuff to that page side: */ side.addChild(someObject); /* Same for odd side. */ side = DisplayObjectContainer(page.odd); side.addChild(new Sprite()); } /* To allow book content (e.g. pages being dragged, plugins, ...) to * exceed the actual width and height set for the MegaZine instance, * disable clipping of child display objects like this: */ mz.clipChildren = false;
Note: you'll still need to have all external data kept available, i.e. the plugins, gui, langs and snd folders. Those cannot be embedded at this time. The above just refers to how to avoid using a book definition file (.mz3).
[edit] Load megazine.swf into own SWF
In some cases you may have a complex SWF in which you want to use MegaZine3, without further enlarging your SWF's filesize by embedding the library (SWC) or sources (from the SVN repository). In these cases, you can simply load the megazine.swf file into your SWF.
var l:Loader = new Loader(); l.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete); l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleError); l.load(new URLRequest("megazine.swf?xmlFile=book.mz3")); // define which definition to use var mz:*; function handleComplete(e:Event):void { var mzMain:* = l.content; // of type 'Main' mz = mzMain.megazine; //of type 'IMegaZine' mz.addEventListener(Event.COMPLETE, handleBookComplete); addChild(l); // add to stage to initialize } function handleError(e:Event):void { // handle loading error trace("cannot load megazine.swf"); } function handleBookComplete(e:Event):void { // engine done initializing, do something with it, e.g. mz.gotoPage(20); }