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.

= ActionScript3 =

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:

Start when visible
To start the SWF when the containing page side becomes visible:

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):

Checking if a page is loaded
To check if a page is loaded, there are two possibilities: use the 's property , or register for  s on a page side. Here is an example that does both:

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.

Note: you'll still either need to have all external data kept available, i.e. the plugins, gui, langs and snd folders, or use the 'fat' swc to embed those.

Load 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  file into your SWF.