localconnection – my life saver

In conjunction with my previous post, where edzis directed me to a post by Grant Skinner called “Failure to unload – Flash Player 9′s Dirty Decrets“.

The situation was, as mentioned before that I’m mixing AVM2 with AVM1, which is not really good way of working, but this time it had to be done like this, in a papervision3d scene I needed to display 4 different swf’s when a user clicked the appropriate navigation button, the loader did it’s job and loaded it’s swf, when unloading the swf the code behind was still active and the GC wasn’t able to delete it properly resulting in a malfunctioning experience.

The localconnection helped me to be able to unload the loaded swf properly, let me show you a few snippets of my code:

At my main stage I load up the different swf’s after the tween has completed.
MainStage (AS3):

1
Tweener.addTween(camera, {x:booklet1X,y:booklet1Y, time:.5, transition:"easeInOutQuint", onComplete:function(){showBook01();}});

Below you see what’s behind the function showBook01

1
2
3
4
5
6
7
function showBook01():void {
	book1 = new Loader();
	book1.load(new URLRequest("loaded.swf"));
	addChild(book1);
	book1.x = 0;
	book1.y = 234;
}

Here my function that resets my stage and removes the swf visually and programatically from the stage
SWF removal code (AS3):

1
2
3
4
5
6
7
8
9
10
function removeClips(e:MouseEvent):void {
	if (removeClipsBut == true) {
		zoom = false;
		book1 = null;
		AVM_lc.send("AVM2toAVM1", "unloadSWF");
		removeClipsBut = false;
	} else {
		//
	}
}

In the original AS2 swf, you just need to create the same connection and listen for commands being sent through it.
loaded swf (AS2):

1
2
3
4
5
6
7
var AVM_lc:LocalConnection = new LocalConnection();
 
AVM_lc.unloadSWF= function(){
	removeMovieClip(mainBook);
	AVM_lc.close();
}
AVM_lc.connect("AVM2toAVM1");

As I was loading 4 different books at runtime the localconnection stopped working after the 2nd click, after doing a bit of research in the livedocs, I found out that the issue was very simple, you can’t open multiple connections using the LC without closing it first, so everytime you create a localconnection don’t forget to close it, otherwise you will get a few cryptic errors without any reference to what you doing wrong.

At the end everything is finished and the client seems happy :)
edzis, thanks for your comment, Grant and Senocular, big thanks to your blogposts, they actually saved my day

Here the links that helped me get along:
Communication through Localconnection by Senocular
Failure to unload – Flash Player 9′s Dirty Secrets by Grant Skinner

Creative Commons License
The localconnection – my life saver by Tiago's Weblog, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 2.5 Switzerland License.

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">