AS3: How to dynamically load movieclip from library without exporting in frame 1? - actionscript-3

I have some fairly large movieclips in the library which need to be dynamically loaded at runtime. I don't want to export them all in frame 1, because that would slow down initial loading of the movie.
I tried putting an instance of each of these clips later in the timeline where they wouldn't normally be encountered. When I then tried to load one from the library dynamically, I was able to successfully get an instance of the movieclip, but its currentFrame property was 0 and I couldn't see anything on the stage. As soon as I enabled "Export in frame 1", it worked properly.
Does this old trick of putting an instance on the timeline somewhere no longer work in AS3?

I have had similar issues with large library assets and to solve my issue I would always just put the assets into separate swf's and load the external swf file when I needed it.
Check out the Loader class 'content' property - http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Loader.html#content
The only downfall to this is managing the assets in separate files.
I hope this helps.

Related

Main menu on game

I have created a menu for my game with buttons and whatnot as a separate .fla file named Menu. My actual game is another .fla file called Game.fla. I was wondering what the best way would be of loading this menu before the game plays?
Thanks.
I would suggest that you create a .swc with your Menu.fla, and then just add that .swc to your Game.fla
If you are using CS6, make sure that you are setting linkage for any MovieClips you want to create instances of in your code.
In the actionscript settings on the library path tab, you can add a .swc that will be included into your .swf when compiling.
In your code, you can now create an instance and add to the display list :
// Assuming MainMenu was a movieclip with linkage set in your .swc
var mainMenu:MainMenu = new MainMenu;
addChild(mainMenu);
I should note that this does not load the .swc at runtime, it includes the .swc to your .swf at compile time. I would recommend this method over loading a .swf in most cases. Cases where I might not, is if there was a large amount of content that wasn't needed all at once. That would allow you to minimize the amount of memory you were using and reduce the .swf size of your main .swf so it loads quicker.
Anytime you are loading .swf's in at runtime you are adding another layer of complexity, so I'd not recommend it unless you do have a good reason to do so. From what you have described, this doesn't sound like a situation where I'd load an external .swf at runtime.
Load the swf output from Menu.fla and keep a check on the progressEvent from loader. Don't show the game screen until Menu.swf is completely loaded and initialized.
Some more details wrt comment from OP:
You can you http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#load%28%29 to load external swfs. init Event would mark that the swf's actionscript has started executing. OTOH, complete event would tell that all the swf bytes have been loaded.

How to get Flex SWFLoader unloadAndStop to unload sound also?

public var swfLoader:SWFLoader = new SWFLoader();
[Embed(source="/some/file1.swf")]
public var file1:Class;
[Embed(source="/some/file2.swf")]
public var file2:Class;
then I do:
swfLoader.load(file1);
Later on:
swfLoader.unloadAndStop(true);
which unloads the video, but not the sound! So I add in
SoundMixer.stopAll();
Which is ok, for a while. Later on, I do:
swfLoader.load(file2);
And eventually, while watching file2, file1's audio will start playing in the background over file2's audio, with no way to stop it! What is the proper way to stop the audio of file1? The way I keep seeing is use unloadAndStop() which I am using. Unless I have to create a new swfLoader object each time?
As per Konrad's answer below, I should stop playing the sound in cleanup events, such as REMOVED_FROM_STAGE, however, how can I stop playing the sound on a swf file that is loaded with a SWFLoader? I don't see an obvious way to do that.
Solutions is bit tricky, because problem is with loaded content.
Garbage collector won't remove sound because it is still playing (so its referenced by Flash Player) but you don't have access to its sound channel (because its hidden similar to private variables in classes). That behavior is totally correct.
Solution: In loaded swf code you should stop playing that sound (in REMOVED_FROM_STAGE or other 'cleanup' event handler). Other solutions (like SoundMixer.stopAll) will work in some cases but not in all.
We often forget to clean after ours applications. Its not problem if they exist same as instance of Flash Player (removing it will clean all memory used by our swf). Problems begins when we load and unload swfs. Ignoring cleanups is fast way to memory leaks.
swfLoader.unloadAndStop(true);
Does remove the swf and the sound as well .
Works fine for me .
Even the documentation says :
Unloads an image or SWF file. After this method returns the source property will be null. This is only supported if the host Flash Player is version 10 or greater. If the host Flash Player is less than version 10, then this method will unload the content the same way as if source was set to null. This method attempts to unload SWF files by removing references to EventDispatcher, NetConnection, Timer, Sound, or Video objects of the child SWF file. As a result, the following occurs for the child SWF file and the child SWF file's display list:
Sounds are stopped.
Stage event listeners are removed.
Event listeners for enterFrame, frameConstructed, exitFrame, activate and deactivate are removed.
Timers are stopped.
Camera and Microphone instances are detached
Movie clips are stopped.

FLVPlayback clip loading delays

I'm building a narrative click-through kiosk app in Flash/AS3. Currently, there are several (10+) locally loaded .flv files that I'm loading into an FLVPlayback component on the timeline. I am experiencing loading delays and am wondering what the best practice / best case scenario for this case. These are all using the "Load external video with playback component" option for Video importing.
So far I've tried implementing it two ways:
One frame, one FLVPlayback playback on the stage named "video_player", and upon the click through / user action to switch the video, I do the following:
var new_flv:String = "next_flv.flv";
video_player.stop();
video_player.source("_flvs/"+new_flv);
video_player.seek(0);
video_player.play();
This results in delays anywhere from a few seconds to 10 seconds.
This is unconventional to me, but I used multiple frames on the timeline. Each frame had an FLVPlayback instance on the stage, each with a different relative path placed in the 'source' property in the component parameters (see http://www.ashleylovespizza.org/stuff/flv_example.png ). The code is switching between frames based on frame label and then hitting play (autoplay is off in the component parameters as well).
var new_flv_frame_name:String = "next_frame";
this.gotoAndStop(new_flv_frame_name);
this.video_player.play();
The issue, again, is that loading is taking a long time. What could prevent this behavior? One long flv that I seek() to different moments of time on the playhead? Can I preload in a separate FLVPlayback instance, similar to double buffering?
Any tips or best practices are appreciated.
Although you have not told me where the flv files are being loaded from (locally or remote), and as you have said you are building a kiosk style app, I am going to go out on a limb here and say that you should almost certainly use Adobe AIR for a kiosk app.
There is no reason for creating more than one FLVPlayback instance, its capable of playing multiple videos using getVideoPlayer(index), its up to you to manage the streams by calling close() on them.
If you are loading files remotely, then using Adobe AIR you can download each video to a local folder using the FileStream class. This will speed up the process of playing back these files.

addchild from externally loaded swf in Air project

I am working on an Air app for iOS and looking for a way to load an external swf and then add/remove the movieclips in that library to the main stage.
Normally, I can use an addchild routine in the document class of the external swf which I can access from the main document class. But since document classes in externally loaded swfs are not allowed in Air deployment, I can't use this method.
Each external swf will contain up to 10 separate movieclips and there are over 25 external swf's I want to load in/remove from the main stage, all containing unique artwork/animation.
Can anyone please suggest a way to do this?
THANKS!
matt
I had same problem i also had around 10 different external swfs with code. So the easiest way for me was to compile every file to swc and included them after in project.

Pass file from AS3 into embedded AS2 wrapper to load

I have a Flash AS3 application that uses FileReference.browse() to request a SWF from the user. If the chosen SWF is AS3, I'm good to go. However, if it's AS2, I need to load it into an AS2 wrapper first (so my app can alter it). All of these files (including my app and wrapper) are intended to exist locally on the user's machine, but the file they select can exist in any directory. So to be clear: Main application (AS3) -> Wrapper (AS2) -> User's file (AS2)
I know how to get the uploaded file's ActionScript version from the Loader's loaderInfo.actionScriptVersion variable, and that's working correctly. My issue is how to pass the file from the AS3 application to the AS2 wrapper so it can load it.
My first thought was to dump the ByteArray from the FileReference's load() function into a SharedObject "cookie". This method seemed pretty bad from a user-experience point of view, but it seemed most likely to work. However, I've been unable to find any method within AS2 to load the ByteArray as a movie (in fact, AS2 doesn't even seem to have a ByteArray class). So the first potential solution to my problem would be if anyone knew of a method for loading a movie from a ByteArray in AS2.
My second thought was to pass the uploaded file's path to my wrapper via the already-setup LocalConnection bridge, and then just have it load the file from that. However, I can't find any way to get the file's path, and my Googling suggests the security model intentionally prevents it. Not to mention, I'm not sure I can load an arbitrary file from the user's machine.
My "hands up in the air; I give up" solution was to just create separate buttons for loading AS3 and AS2 files (leaving it up to the users to guess which it is!) and have the AS2 button actually within the AS2 wrapper. However, it looks like AS2 doesn't have a file browsing uploading API, and the PHP-hybrid solutions I've found aren't an option (because this is meant to be run locally).
So, I would be eternally grateful if anyone could point me in the right direction for solving any of these three roadblocks. Alternative workarounds are of course welcome.
(Edit)
Ok, I found the documentation for AS2's version of the FileReference class. It supports the same file-browsing capability, but does not support directly loading the selected file into the SWF.
However, the security sandbox doesn't seem as strict for local files as I expected, and it looks like I can load any SWF on the user's machine once I have a path to it. So I should be able use JavaScript and an HTML form with a file input to get and pass the file path to my application. It's not ideal having to do all of this from within a web browser, but it should work. If it turns out satisfactorily I'll submit it as an answer.
(Edit 2)
Scratch the HTML-form idea. Looks like the path is hidden from JavaScript for the same reasons Flash hides it. The only option I can think of now is to have the user copy and paste the path to the file...
After reading over your post, you may be able to retry one of your previous attemps with some new information. Actionscript 2 DOES have a method for looking up files from a browser, same as AS3 does. AS2 also has a FileReference class. Check out the documentation here:
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001040.html
Also, here is a tutorial:
http://markshu.ca/imm/flash/tutorial/fileReference.html
Well, all of my other leads have dried up, so I'm submitting the two answers that will actually work, although neither is ideal:
A) Use Adobe AIR, which will give more access to the filesystem (such as for getting path info) at the cost of requiring the separate AIR runtime to be installed.
B) Have the user enter the path to the file themselves (cumbersome for the user)