AS3: How are SWFs are compiled from external .as class files?
All,
In the Flash app I'm building, I have a "main" SWF that will load a variety of "child" SWFs.
All of the AS3 code for both the main SWF and child SWFs is contained in external .as class files.
In other words, each movie has it's own document class .as file.
There are also a number of .as class files for all of the common elements (e..g, UI buttons and controls).
So - for example - both my main SWF and all child SWFs use the "MyCommandButton" class; the code for this class is in MyCommandButton.as, which all SWFs reference (import).
Here's what I don't understand.
(Let's say that the code in MyCommandButton.as draws buttons that are red.)
First, I compile ALL of the child SWFs.
Next, I change MyCommandButton.as, so that the buttons it draws are blue, instead of red.
Then, I compile the main SWF.
When I run the app (main.swf), I would expect all of the buttons in the main SWF to be blue (since that's what MyCommandButton.as specified when main.swf was compiled), and all of the buttons in the child SWFs wil开发者_运维技巧l be red (since that's what MyCommandButton.as specified when they were compiled.)
Instead, ALL of the buttons are blue, which means the change I made to MyCommandButton.as affected all of the child SWFs, even though I didn't recompile them.
Why is this? Is there a simple explanation for why this occurs?
Many thanks in advance!
It's due to the way Flash handles loaded byte code. Specifically, look into the ApplicationDomain documentation.
Every application domain, except the system domain, has an associated parent domain. The parent domain of your main application's application domain is the system domain. Loaded classes are defined only when their parent doesn't already define them. You cannot override a loaded class definition with a newer definition.
Note the "Loaded classes are defined only when their parent doesn't already define them" section. The MyCommandButton class from your child SWFs already exists in the Main SWF (the parent ApplicationDomain) and is thus not used.
This is particularly important to prevent hacking. Think of the possibilities if you could inject your SWF into someone elses' dynamic application and start re-writing all their classes.
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e07.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ApplicationDomain.html
精彩评论