Hi,
I'm trying to start off a as3.0 project with nothing in the maintimline, I want my default actionscript class to start things off. Like a main in C, so is there a main like function in AS3.0 ?
Hi,
I'm trying to start off a as3.0 project with nothing in the maintimline, I want my default actionscript class to start things off. Like a main in C, so is there a main like function in AS3.0 ?
The Document class is the closest to what you want. Document class info. I tend to keep the document class to a bare minimum.
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Document extends MovieClip
{
public var _main:Main;
public function Document()
{
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
//all initialization goes in here!
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
initMain();
}
private function initMain():void
{
_main = new Main();
}
}
}
Note that calling another Main class is just a personal preference thing. Also the event listener is quite important if you are trying to reference items on the stage.
I have a few modifications to the answer above which might be useful.
The listener is important for referencing things on stage only if the SWF could potentially be loaded in as a module into another application. If this document class is guaranteed to be the root document class, then adding an event is essentially pointless. I've modified the answer above so that it take the faster route if it is the root SWF, without braking the potential for modularity.
I'd recommend looking into a framework like RobotLegs to organise your code. Most people who move into a more interactive scenario think that they don't need them to start with, and end up realising that they exist for very good reasons - namely that dependencies tend to get messy fast in this line of work. If you chose RobotLegs, your 'Main' class would be the framework Context.
In any event, to be useful your Main class will need access to the display list, so you'll need to pass in a reference to the Document class. Pass it in cast to DisplayObjecContainer though, to avoid dependencies on Document in Main.
You also don't need to extend MovieClip since there's no timeline here; Sprite will serve just as well, and the class will be fractionally lighter. There's no good reason for exposing the main variable either - if you're going to use the SWF modularly and expose an API in the document class, you don't want to expose the Main class anyway, you want to expose an interface. (Unfortunately, you can't usefully interface Document and load it in as a modular SWF - or you couldn't at least for Flash Player 9 - as there is a bug related to this process: blog post on problems interfacing a SWF
With these modifications then, the above answer becomes:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Document extends Sprite
{
private var main:Main;
public function Document()
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
}
private function onAddedToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
init();
}
private function init():void
{
main = new Main(this);
}
}
}
But your main class wouldn't know the Document type, so:
package
{
import flash.display.DisplayObjectContainer;
public class Main
{
private var viewComponent:DisplayObjectContainer;
public function Main(viewComponent:DisplayObjectContainer)
{
this.viewComponent = viewComponent;
...
}
}
}