In Flash, despite publishing for "export in frame 2", many classes seem to be in frame 1, why? - actionscript-3

I decided to add a preloader, so I wrapped the content of frame 1 in an object and moved it all to frame 2. I assigned the document class to that new object in the library, and cleared the document class from the publish settings, effectively detaching the document class from the main timeline.
I went into Actionscript Settings and set the "Export classes in frame:" option to "2". If you look in the library, each symbol's option for "export in frame 1" now says "export in frame 2", so I have to assume it means what it says.
Despite frame 1 being empty, and despite the ActionScript setting to export classes in frame 2.... the generated size report still reports that most of the classes are exported in frame 1.
I have one SWC file referenced which is set to be "merged into code", but shouldn't that data be defined in frame 2 as well? Again, the size report is showing most imported definitions defined on frame 1.

I figured this out rather quickly.
The problem seems to be having an instance of the main document class on frame 2. By simply having an instance there, its creating a variable of that type on the main timeline, hence it needs to have that definition loaded to instantiate the main timeline for frame 1.
The solution is to make sure that the entire main timeline does not in any way reference any classes you don't want loaded in frame one, especially your document class. Instead, instantiate your main document class via code on frame 2 with the "getDefinitionByName" utility function.

Related

AS3: Optimizing my Flash's internal preloader

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.
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()

How to do transition to next frame

I'm a beginner in AS3 and Flash.
There are index.swf and intro.swf, which is loaded in first frame of index file.
In intro.swf I placed a button, which should send command to index.swf main timeline to go to frame 2.
If i use parent's construction, i will try to access level that is not present in intro file at the moment of compilation. So, how can I do it?
First of all you should define which method you use to load one swf to another. On this depends what will be the reference to the top level.
If you used flash.display.Loader then from MainTimeline of loaded swf to MainTimeline of loader swf (if you load not inside so object but create Loader itself by code on Maintimeline - reference will be
MovieClip(parent.parent)
Loaded swf root stored inside Loader instance at content property, that why there is two parent. And data-type convertion should used because parent return reference as DisplayObject and thats block you from access it properties.

external document class injected into a timeline based Flash app

Here's the situation:
Making an app in Flash CS6 that has 11 frames. Some frames have actions dependent to that frame and then there are some global actions. There are no external classes, everything is incorporated into one flash file.
Now I want to incorporate AdMob and found an offer that should work for me, but it calls for setting a document class linked to external AS file. Doing this messes up my timeline based actions.
I am more front-end that back-end when it comes to designing/programming. So, my question is: How can I incorporate this external admob actionscript into my flash document file without messing up my current scripts? Is there a way to remove it's package attributes and put it on the timeline?
Hope this description wasn't confusing.
Thanks!
Some brief about document class & classes & timeline :
Consider the whole stage as a movieclip. Now the document class would be a user defined, external class for the stage movieclip. When you don't specify the document class, flash uses a predefined one, which derives a plain movieclip class. The frames added from the IDE are converted as calls to addFrameScript and the script in the frames are scheduled to run at an interval as per the frame rate (timeline). Even the shapes you draw, images you add, everything goes in as code in the frame scripts of the stage movieclip.
Now if you add a movieclip onto the stage, the above process is repeated for it as well. You may set a class or derive from an existing class for any movieclip. The class will be initialized when the movieclip instance is created, Be it frame script or place the movieclip itself in the frame.
Now to your question.Any frame script can access the external class as well. So if you have an external class named AdMob in a file AdMob.as, lying next to the fla, you may call it anywhere, in any frame as:
new AdMob()
However you must take care of the package name & path. So if the package for AdMob is abc.bbc.AdMob then the location of the as file should be abc/bbc/AdMob.as
You may import the whole package as :
import abc.bbc.*;

Internal AS3 preloader & stage issues

I need to create a single SWF with no external files, so I'm trying to add an internal preloader to my Flash project which has [embed] assets. I know [embed] causes problems with preloaders because it puts the assets on frame 1. I have tried the solutions recommended in these posts, where you set the document class to your preloader class:
Preloader for SWF with embed bytearray
How to create Preloader in AS3
I can get it to work, but ONLY if I comment out any lines of code that involve the stage, otherwise I get an "Error #1009: Cannot access a property or method of a null object reference." Those lines are essential though, so does anyone know how to fix those errors with the stage?
You haven't posted your code or your fla, so all I can do is share what works for me.
First, I wouldn't use Embed. Instead, use a swc. I have found that Embed can be unreliable as far as actually getting the entire asset in there (at least when publishing with Flash Builder + Flash Pro, which is my workflow).
Once you have your assets in a swc, try the following steps:
Set your export frame to Frame 10 (or any frame other than 1--I like frame 10 because then you can read the label that says "Preloader")
Put your actual content on frame 11. You can structure this a lot of ways. Since I program to Interfaces, I give whatever is on frame 11 an instance name and then use a setter to determine that my "first thing" has been placed on stage. I can get away with this because my main Document Class just knows the definition of the Interface, not the full implementation of the Class, so the Class does not need to load for the main Document Class to work. You probably aren't truly using the timeline and probably didn't program to interfaces, so you'll probably just set the base class of the symbol that's on frame 11 to the main logic of whatever you're trying to do.
Put your preloader graphics in Frame 1. I'm not sure why your stage references are so important. I, personally, don't use any logic in the preloader. Instead, I use a spinner that spans frames 1-10 (plus the word "Loading...". The spinner just spins while the classes load. The embed frame acts as a temporary "stop" that just holds the timeline back until those classes have been loaded. Once the classes have been loaded, the timeline will act like you called play() on it. So it really can be that simple. If you need it to be more complicated, give one of your preloader graphics an instance name and set up a getter/setter pair for it, then use the setter to trigger your logic that accesses the stage. You are pretty much guaranteed to have a valid stage at that point.
Word of warning: if you did make use of the timelime, you will get strange results if you try to jump to a frame that isn't loaded yet, so make sure to check to see if a given frame is loaded if it's near the end of your main timeline and your main timeline is heavy with assets before calling goToAndPlay() or goToAndStop().
Some references that might help you further:
Preloaders vs as 3 (I'd recommend you read the entire series this is part of. This is an amazing series I wish I'd found 3 years ago)
Solving the Frame 2 Problem Presentation and code
Combining the Timeline with OOP The example code for that is here (long story)

Only the loader images are being exported in frame 1 but the loader still only shows up at 100%

As the title says, the file loads correctly but the loading screen only flashes up at the end.
The only thing being exported in frame 1 is the loader image, and that is extremely small.
Is it possible that there's a queue of things being loaded and the loader image is at the bottom of that queue? Since that was one of the last things added to the project
If your preloader only shows up after the file has loaded, this means that you still have other objects being linked onto Frame 1. Flash is very picky about this, and it's easy for things to be yanked onto Frame 1 regardless of your settings. When you compile, Flash builds a dependency graph to determine what items are needed on each frame. If it thinks an asset is needed earlier than the export frame setting, it will ignore the setting and push the asset onto that frame. In particular, any class that your document class directly references will be automatically yanked onto frame 1.
Checking "Generate size report" in File, Publish Settings, Flash can help you see how much data is being exported onto Frame 1. Here are some tips to ensure that everything is linked onto the proper frame:
In Publish Settings, Flash, ActionScript Settings, make sure that "Export frame for classes" is set to 2 or higher.
Ensure that library symbols say "Export on frame 2" in their linkage properties. Older versions of Flash may export them on frame 1, regardless of the class export frame. In this case, you'll have to do the oldschool method of unchecking the "Export on frame 1" option, and manually dragging these symbols onto the timeline on frame 2.
Do not directly reference classes from your document class or on the main timeline. The Document class and all the assets it references are always placed on frame 1. Anytime you do var f : MyClass; in your document class or on the root timeline, then you are referencing MyClass, and Flash will automatically yank it onto frame 1.
To avoid directly referencing your main app class in your preloader, you want to instantiate it indirectly, using something like this:
var gameClass : Class = flash.utils.getDefinitionByName("Game") as Class;
var game : Sprite = new gameClass();
In this case, your Preloader becomes the document class, and indirectly creates the Game class when the SWF has loaded. This avoids any direct references to Game and its content.