Working with multiple flashpunk swfs


(Linck) #1

I’m working on a project of a game that has some minigames inside. The main game is made with flashpunk and it loads the minigames as separate swfs. Sometimes the minigames are also made with flashpunk, and that’s a problem apparently.

If the main game loads a minigame that is not made with flashpunk, everything goes well. Both swfs keep working (listening to key inputs and enter frame event). But when the minigame is made with flashpunk, the main game stop working.

I’m loading the external swfs with regular URLRequest + Loader flash classes:

urlRequest = new URLRequest(swfLocation);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
loader.load(urlRequest);

public function swfLoaded(event:Event):void {
	FP.stage.addChild(loader);
}

Both the main swf and the minigame swf are made with flashpunk so their main classes extend engine and they have their own worlds and stuff.

Does anybody know what could be possibly causing this issue? What exactly would cause both swfs to not work individually? Is there something to do with them using the same stage or something?


(Zachary Lewis) #2

I’d bet it’s some of the static variables that the framework uses.

I’m not super familiar with loading external movies, but I do know that they all share a Stage, so multiple references to the stage could be causing issues.


(Linck) #3

There can only be one stage on the flash player or each swf has a separate stage?

I made a test in witch I added a key input event listener on the main swf. In the World of the main swf I added and event listener like this:

FP.stage.addEventListener(KeyboardEvent.KEY_DOWN, myFunction1);

And then I used this key handler instead of the flashpunk one. And it works. The main swf listen to the key even if there is another swf using flashpunk loaded and running. But that was just a test I did in order to understand better how it works. That’s not what I want.

Another thing I’ve tried is changing something in the Input class from flashpunk.

There are these lines in the Input class:

FP.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
FP.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
FP.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
FP.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
FP.stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
FP.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

The listeners are being added to FP.stage. I have tried to add the event listeners on FP.engine instead, as engine extends MovieClip, because I thought maybe the issue was that both engines shared the same stage and were adding event listeners to the same stage causing some sort of conflict. But adding to the engine the inputs stopped working, I don’t know why.

Another question: If its the same stage, wouldn’t addEventListener function just ADD the event listener and not overwrite others?


(Zachary Lewis) #4

That’s a good question. I bet you could write a function to trace and compare the stage references of each of the movies and see what Flash thinks is happening.


(Linck) #5

I have actually found the answer on the actionscript documentation. Although I didn’t find when I first looked in the Stage documentation. There is a line in the documentation of the DisplayObject that says that there can only be one stage:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#stage

Also, I made some more tests. I thought maybe the problem could be that the stage is static for some reason, so I made the FP class a singleton. Didn’t work.

Other than that, I have tested if I can add more than one event of the same type. So I tried adding two KEY_DOWN events and two ENTER_FRAME events. Both the functions set to each type ran perfectly. So it’s not an issue with one event overwriting the other when they’re added.

Any more suggestions?