0
votes

I have a game that I've made that runs in two files, one the preloader and one the swf. When I use these two files together they work fine, however some flash portals require only one SWF, so I'm trying to find the shortest-path way to convert it to use only one.

The game swf uses a Document Class that includes the package, class constructor, and tons of functions, and runs fine. I have lots of movieclips on the stage that I refer to in my code. The code is something like this (abridged):

package  {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
...
    public class Game extends MovieClip {       
    var EnemyArray:Array;
    var HeroArray:Array;
...
public function Game() { // class constructor
        EnemyArray = new Array();
        addEventListener(Event.ENTER_FRAME,onEnterFrame);
                    mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
...
}
public function KeyPressed(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.SPACE)
        {
        attack();
...
            }
} // End of class
} // End of package

I also have another swf, my preloader, which has the following code in frame 1:

import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.Event;

var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
var myURL:URLRequest = new URLRequest("Game.swf");
myLoader.load(myURL);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

function onLoading(event:ProgressEvent):void {
    var loaded:Number = event.bytesLoaded / event.bytesTotal; 
    percent_txt.text = "Loading: " + (loaded*100).toFixed(0) + "%";

}

function onComplete(event:Event):void { 
    myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
    myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
    this.addChild(myLoader);
}

I've been through the gamut of solutions online and none of them seem to work with the way that my code is set up, such as: How to create Preloader in AS3

I tried his option 1 by taking all the code in my Document Class other than the package and constructor and pasted it into frame 2. However it returns "Error #1009: Cannot access a property or method of a null object reference." for any references to items on the stage, such as:

  mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);

As he mentions, "This works well if you organized your project in a way that the most of the assets (images, etc) are in the Flash IDE Library and are not loaded on the first frame (you can check that in each library item's properties)." My project is NOT organized in such a way.

What is the easiest way for me to reorganize my FLA/code so they can have a preloader in a single swf?

Thanks so much for your time and help!

1

1 Answers

0
votes

I use FlashDevelop (no FLAs) so I'm not sure if it'll work for you but I use the solution described here.