Archive for January, 2010
More on preloaders: passing the loaderInfo
Posted by zedia.net in ActionScript 3 on January 19th, 2010
I previously made 2 posts on the topic of preloaders (The right way to do a preloader in AS3, External Preloader; more complex cases), well this post will be a continuation of those. And one that I believe not everyone will agree with me. I know that because I didn’t agree with it at first but the more I thought about it the more it felt right.
When it comes to code I’m a bit of a fascist, I have trouble accepting habits of other coders if they are not the same as mine. So when I see something different my first reaction is to frown upon it (I am talking just about code; I am a very open minded person). When I first saw this mean of passing variables from the preloader to the main application that a coworker was doing I didn’t quite like it.
In the post about the more complex preloaders, I showed how to use an interface to pass data from the preloader to the loaded (main) movie. Now this part of the code is still the same. What changes is that instead of passing the flashVars (variables that are passed to the flash from the html embed code or javascript) individually inside fo the init method of the Main class (also in the interface), we pass them all together by giving the root.loaderInfo instead.
I already know what you are going to say: this is not strictly typed so it is bad. I know, I know, but if you think about it a bit you see that at some point the flashVars are not typed anyway; when they transition from html to flash. So what is the harm of perpetuating this just one level more? In the init method inside the Main class, the first thing I do is that I type the parameters passed, so I do end up typing my variables.
Now, you’re going to ask what do you gain from this? Well, since the preloader is an external file, every time you are going to pass more variables to the Flash from the HTML, you will have to modify 3 files : the preloader.fla, the IMain.as and the Main.as. Now if you pass the loaderInfo instead of the individual flashVars, you will only need to modify the Main.as since it is there that you type the variables. You completely bypass the preloader, which in a way make sense since your preloader doesn’t need to know about your application, all it does is to load it. once your preloader is completed you don’t ever have to touch it again.
Here is some code to illustrate this. In the preloader :
var mainContent:IMain; function onLoadComplete(event:Event):void{ // this would be the function that the loader would call when the loading is completed mainContent = IMain(loader.content); addChild(Sprite(mainContent) ); mainContent.init( root.loaderInfo); }
And in the Main class :
package{ import com.zedia.interfaces.IMain; import com.display.Sprite; import com.display.LoaderInfo; public class Main extends Sprite implements IMain{ public function init(loaderInfo:LoaderInfo):void{ var flashVar1:String = String(loaderInfo.parameters.flashVar1); var flashVar2:Number = Number(loaderInfo.parameters.flashVar2); //do something with the FlashVars } } }
The post of the year
It is that time of the year where I look back and ponder. As I had foreseen, 2009 was really a great year (at least professionally but that’s another story). If my skills improved a lot in 2008 they continued to do so in 2009. I got to play a great deal with Papervision3D (roar4milk, Holiday Humanoes) and I got to modify my project workflow a lot too (I plan to make a presentation on that this year).
In numbers, I got to write 62 posts ( a bit less than once a week) and got 478 comments since September 2008. As for traffic, I am now getting 25 000 visitors a month, this is 10k less than the goal I set myself a year ago, but I don’t think it is so bad; my traffic is still growing steadily month after month. I did not write another article for InsideRIA like I wanted to, but I wrote 2 that got printed in FFDMag ( one of those I intended for InsideRIA but someone wrote on Google Analytics for Flash before me). What I am really proud of is that I got to give 2 presentations; one online and one in person at the Montreal Flash User Group. This is something I want to do more in 2010, 3 or 4 speaking engagements would be really nice. I already got an article nearly done so if I could get 1 or 2 more I would be happy. Lastly, I my traffic could increase to 35k visitors a month I think that would be enough for me. While we are at this, if I could be part of writing a book on ActionScript, that would just be the craziest.
Anyway, what I wish for the coming year is that I keep doing what I am doing. If I keep up the good work, good things will come my way, as simple as that, it worked fine in the past and I am pretty sure it will stay that way, naïvely. Blogging is all about keeping doing it.
May 2010 be as good for all of us as 2009 was good for me. Happy new year!


