Reputation: 632
I'm trying to optimize my Flash project's internal preloader. The preloader is on frame 1, and when it's done, it gotoAndStop
's to frame 2, which instantiates my Game class. My total SWF file size is 1847 KB, but according to the size report, my frame 1 by itself is still 388 KB. I was hoping I could somehow bring that down further.
If I comment out the code that refers to my Game class on frame 2, my frame 1 drops down to 68 KB. I'm not sure why commenting out the frame 2 stuff affects frame 1, but it would be nice if I could keep that small frame 1 and still instantiate on frame 2. Is there anything I can do, or is that as good as it gets? My Game.as is 265 KB.
Upvotes: 0
Views: 73
Reputation: 18747
The size of the *.as file should do nothing with the resultant SWF size. You can optimize frame 1 by excluding static data out of there, especially bitmaps, and also avoid direct reference to Main
class or any game object derivatives. See, whenever you include a direct link to a class (say, in form var a:SomeClass;
) the class description together with embedded resources, linked classes and all the stuff required to statically link the class is also compiled into the first frame, increasing its size. In case you so badly need to link an object out of subsequent frames, make sure the SWF has been loaded fully, or at least (if you decide to split frame 2 into several) the frame with that class is loaded, then you can call getDefinitionByName()
with full qualified path to the instance and receive the class info out of loaded data. Then you instantiate an object of that class using the var returned as class template, and use it as normal since then.
An example:
// provided you have a link to `PlayButton` in your first frame
// var playButton:PlayButton=new PlayButton();
// ^ the code to replace
var playButton:Sprite; // MovieClip can also do, if PlayButton is one
var playButtonClass:Class=flash.utils.getDefinitionByName("PlayButton") as Class;
// stuff full package path in this call ^^^ e.g. "mygame.PlayButton"
if (playButtonClass) playButton=new PlayButtonClass(); // otherwise catch errors
The manual on getDefinitionByName()
Upvotes: 1