Skip to main content
Participant
June 12, 2009
Question

Importing a .swf game into a flash website at runtime.

  • June 12, 2009
  • 1 reply
  • 1192 views

Hi,

I am a first year uni student so please bear with me. Using actionscript 3 how do I imort a .swf game into my flash website at runtime, this is the code that I have so far:

var myLoader:Loader = new Loader();

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadComplete);

function swfLoadComplete(loadEvent:Event)

{

        addChild(loadEvent.currentTarget.content);

}

myLoader.load(new URLRequest("Hangman.swf"));

However when the game is loaded it doesn't register the keyboard presses needed for the hangman game to work.

Any help would be greatly appriciated.

Cheers

Hynesy

This topic has been closed for replies.

1 reply

Missing Code
Inspiring
June 12, 2009

Here is the code that I have used to load an external SWF file to my site:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showLoader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishedLoading);
loader.load(new URLRequest("myGame.swf"));

function showLoader(e:ProgressEvent): void {
    var percent:Number = e.bytesLoaded / e.bytesTotal;
    viewPercent.text = Math.ceil(percent*100).toString();
}

function finishedLoading(e:Event):void {
    removeChildAt(0);
    viewPercent = null;

    addChild(loader);
}

It looks pretty much just like yours except I have it show the percent loaded (which isn't neccessary).

Could you post the part where you are using the keyboard? Becaue your load looks proper.

Participant
June 12, 2009

Thanks for the quick reply, inside the game,

stage.addEventListener(KeyboardEvent.KEY_UP,inputKey);

Do I need an event listener outside the game?

Cheers

Hynesy

Missing Code
Inspiring
June 12, 2009

The keys not working really stumped me at first too. In order to have the external

swf recognize stage and be able to use the keyboard you need to add keyword

"this" in front of addChild. Here is really simple loader code:

/* My Loader: Loads the file, nothing more */

var loader:Loader = new Loader();

this.addChild(loader);
loader.load(new URLRequest("myGame.swf"));

And then you should be able to proceed as normal with your keyboard actions.

Here is my code for a single key press moving a symbol:

/* External File: myGame.swf*/

var leftArrow:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveSymbol);

function keyPressedDown(event:KeyboardEvent) {
    if (event.keyCode == 37) {
        leftArrow = true;
    }
}

function keyPressedUp(event:KeyboardEvent) {
    if (event.keyCode == 37) {
        leftArrow = false;
    }
}

function moveSymbol(event:Event) {
    if (leftArrow) {
        mySymbol.x -= 5;
    }
}

Everything worked fine in my test. Let me know if everything works for you.