0
votes

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.

2
Your not getting any errors? To me it sounds like a null stage reference in your Game.swf. Can you think of any place where you might be trying to talk to the stage before a stage reference exists? The stage reference will not exist until addChild(myGame); executes.Tyler Egeto
Do you have an event listener for an Event.ADDED_TO_STAGE event in the constructor of FunkyChickenGame? This would allow you to listen for when your game is added to the stage (via your client's addChild() method), then you can initialize your game when that event is triggered.wpjmurray

2 Answers

1
votes

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);
0
votes

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.