views:

2495

answers:

2

We're developing a small Flash gamelet for an European client of ours, who require us to develop our Flash game in such fashion that they can load our SWF game into their ActionScript 3 project, dynamically.

Basically they have a console that will load many other Flash games, amongst our own, depending on which button the player presses on a "Choose Your Game" screen.

Basically, we have a FLA project that we're authoring in Flash Professional CS4 and everything is basically a straightforward Flash game. When we test the game via Ctrl+Enter or run the compiled SWF file by double clicking it, all works well, the game executes perfectly and everybody is a happy bunny.

What we need to grasp is loading our SWF into our clients AS3 project, which is basically an external ActionScript 3 project created in Flex Builder 3. The client posted us the following code to load the SWF:

var myGame:FunkyChickenGame = new FunkyChickenGame();
addChild(myGame);

...which gets executed in the ActionScript 3 application constructor of the clients app, or perhaps an event handler for a button pressed on the "Choose Your Game" screen.

We tried creating a blank AS3 project in Flex Builder and tried loading the SWF as per my snippet above. All our traces within the external SWF's document class are showing up as expected in the Flex Builder console view, so the code is running perfectly.

The problem we're experiencing is that despite calling addChild(myGame)...we're not seeing any graphics, just the default background color of the encapsulating AS3 project.

Note however, when we run the SWF by double clicking the Game.swf file in Windows, it all executes perfectly, the game works without flaw and glitches.

Any help on this topic would be highly appreciated. Thank you in advance.

+1  A: 

What you need to use is an instance of the Loader class, like so:

var loader:Loader = new Loader();

// you'll need to write a method named onLoaded to capture the COMPLETE event
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);

loader.load(new URLRequest("game.swf"));
addChild(loader);
Branden Hall
Thanks Branden. The onLoaded event, would that in turn just be empty? So in other words, can I do this: loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function():void()?
Joe Zephyr
Sure, but if you're not going to track the event there's no need for that line in the first place. You only need it if you want to track when the load is complete.
Branden Hall
A: 

creating an instance of a class is as wage as it can get. your client has a funny way of posting references.

however FunkyChickenGame may be a class created by the client that can launch your game. maybe the games are embedded in their flash app, but that would mean long loading time. if indeed many games are "loaded" this way.

my advice would be that you don't use stage, root or whatever properties that could link to the main app instead your game, and leave the client to decide how he would like to load it.

encoder