Best way to embed Flex-based swf into pure-AS3 project? - actionscript-3

I'm working on a AS3 project that required a lot of UI, since Flex was used for time-saving for this part we ended up having a Flex Project (UI) and pure AS3 one (All BackEnd) or, in other words, two different .swf files.
Currently I can successfully comunicate both of them using a common interface and by loading the UI .swf into the AS3 .swf using a Loader class:
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest(“CustomUIModule.swf”);
The actual code contains listeners for SystemManagerHandler so Flex doesn't complain.
Since we are delivering two different files it is not really practical whenever we release a build of the project. For this case the meta tag [Embed] seems to be a better way to achieve this (With no satisfactory results to date).
With the above information (hopefully I made myself clear enough) what would be the best approach to ultimately generate a single swf? Or how should we properly implement the [Embed] tag for Flex swf?
Thanks in advance.

I try to avoid loading SWF into another SWF It just doesnt seem right to me, especially if I have control over all the SWFs. If it was me I would integrate everything into one fat flex project. After all isn't this what OOP is all about.

Related

Creating Preloader for SWF in AS3 Flash Builder

I'm a relatively new developer in AS3 using Flash Builder. My objective is to create a quick/fast loading small SWF preloader for a larger (30mg) SWF application. I can't include the larger SWF inside the preloader, but I can use external interface calls to get jquery to tell me what the speed of internet the users are using and figure out how long it should take to load based on the size of the larger SWF.
Right now, we have a large web application that takes around 5 mins to load if the client has a slow internet connection. So, we wanted to use a small SWF preloader that will display a loading circle or loading bar, and display options if the SWF takes longer than 5 mins to load. Kind of like error handling. For instance, for Safari, sometimes the player actually has to click the SWF in order for it to load. Is it possible to have a button on the SWF that will make it start loading?... Is it easier to create the preloader in jQuery? I'm just wondering if it's just going to create more issues using a SWF to load a SWF. Any ideas?...
Any ideas on how to tackle this beast? I've read almost every article about preloaders but there is nothing that references preloaders created in Flash Builder.
I can't use Flash Professional or timeline. It has to be in Flash Builder.
So I tried to use the below tutorial and I wasn't able to actually get anything to appear when I test it. I'm getting an error at the "run()" function:
Error: Error #2136: The SWF file http://local.myproj.com/MyProject.swf contains invalid data.
at Preloader/run()[/Development/MyProject/src/Preloader.as:109]
at Preloader/onEnterFrame()[/Development/MyProject/src/Preloader.as:46]
References:
http://fortheloss.org/how-to-preloader-in-flash-builder-4-7/
I worked until two years ago with a lot of these things and I know I have a few good scripts in my databases, but you are talking about Preloaded & activated loading were as in a preloader it is out of the hands of say a visitor what he or she can or cannot do, and in using a button (possible of course) the visitor has a choice to download or not! From memory there was never really a lot published yet over years of using Flex Builder I have collected some good AS3 scripts and modified them (including a progress-bar - but without time Indicator) to download in web Apps. or FLEX AIR etc. with AS3.
The small problem would only be to adapt any of it from mx to fx depending on what FLEX Builder you use!
I used my scripts mainly to download high resolution Images, Calendars, SWFs etc. with up to compressed 22 mB! Anyway, if Interested I have to look up my FLEX databases & Application to get it to you! rgs Aktell

How to implement Caching while loading SWCs dynamically

I'm loading dynamic loading of all my SWCs in my master SWF, in order to load master swf faster, however now I need to cache all my swcs in local machine to speed up things.
private function loadAssets():void
{
swcObj=new Object();
swcObj.swcPath='assets/swc/1.swc';
swcObj.className="Part_0_1";
swcs.push(swcObj);
swcObj=new Object();
swcObj.swcPath='assets/swc/2.swc';
swcObj.className="0_2";
swcs.push(swcObj);
swcObj=new Object();
swcObj.swcPath='assets/swc/3.swc';
swcObj.className="0_3";
swcs.push(swcObj);
}
Then I'm using this array to use all the classes in my project, but I have no idea how to cache these swcs for faster use, if anyone have idea, please share.
In fact, the browser does this pre-caching for you, you don't need to produce extra efforts. So, just load them normally and don't worry about caching. You can, however, motivate the user to increase their local browser cache in order to potentially lessen time spent on waiting while your assets are loaded, but this won't help should the user watch three tons of YouTube each day.
SWC files are not intended for dynamic loading.
They are static libraries that can be linked in a swf using
-include-libraries and library-path options of mxmlc or - since you seem to be using FlashDevelop - SWC Include Libraries and SWC Libraries in Project>Compiler options
. SWC's may hold code (classes), assets (symbols/bitmaps/sounds...) or combination of the two.
Loading assets dynamically is done through flash.display.Loader
you may use the Loader as a simple DisplayObject instance that you add on stage:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#includeExamplesSummary
, or use its ApplicationDomain as a library of Class definitions that will allow you to create instances at will :
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ApplicationDomain.html#includeExamplesSummary
Caching from the browser will be sufficient in most cases, unless you have VERY specific needs
In the end, there is different ways to optimize loading times, one of which is having a small swf acting as loader/home menu, and loading rest of the content on demand like you seem to try doing, but you can also create a single swf with several frames, which will be "streamed" by flash player, example:
1st frame as small as possible holding just a few kb for a splash screen/logo/loading/whatever you want, to make the initial blank screen as short as possible, then second frame holding the main content. You can event extent this system with for instance
two levels of preloader: a first tiny frame with just a logo, second one with progress bar, and full background if needed
split content like home screen/menus in a frame, gameplay in the another, if you are making a game, so that gameplay continues loading while you are already displaying the menu

FlashBuilder: how to embed graphics for preloader?

I develop a project in FlashBuilder. The graphics and UI elements i create in the Flash IDE and give them Export Classnames. then I put the SWC in the library paths of the FlashBuilder project and create the UI elements by instanciation.
Now I want to add a Preloader for the application. I follow this article, which works:
http://pixelpaton.com/?p=4642
My question is now: I also need some graphics for the preloader. But how do I ensure that the graphics for the preloader will be loaded first, such that the preloader class can start as soon as possible?
The compiler will figure out the dependencies for your preloader class, and load them first.
For example:
public function Preloader()
{
addChild(new UIElementFromFlash());
}
Flash Builder will know UIElementFromFlash needs to be loaded before Preloader. It will also load Preloader before your main class and its dependencies as long as you have the Frame metadata tag from the article.
I'd suggest to make 2 swf files if it's possible. Make a loader.swf which loads your application swf and displays the progress and your loading animation/graphics.
See an example here: http://www.republicofcode.com/tutorials/flash/as3preloader/
Then you'll have to add URLLoader class and load your application.swf.
Try looking here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html
Like Gio suggested, having two SWF files is the best practice.
However, one other solution is to use the -frame two argument of the Flex compiler. All your definitions (code and assets) will be added to the second frame of the movie, allowing your main class to listen for the loading progress of the application and display animations.
Be careful to not reference anything from the main application in your first frame, to prevent adding more weight to it.
More detail on this technique here:
http://www.andymoore.ca/2009/08/flexsdk-3-3-how-to-make-a-flash-preloader-in-as3/

How many levels deep can you nest swf's - will 3 work - like child inside parent inside grandparent?

The code at the bottom of this post nests an AS2 "player" inside a Flex app (AS3). Look at the part that says "[AS2 player swf]". That AS2 player swf has almost no code - just one function that loads an asset from a remote server. Very simple:
this.onLoad = function(){
content.loadMovie("http://URL_TO_REMOTE_SWF");
};
And it has no other assets. Just one MovieClip - "content".
So we've got a Flex app that loads an AS2 swf that loads a remote swf. No dice. Remote swf doesn't load and display. No errors reported.
Important: The AS2 app on its own works. If I go navigate to it in my file system and double click, remote swf does indeed load and display. So I've established that the unwrapped AS2 swf player works and does indeed load remote swf's. Something about nesting that player inside the Flex app creates a problem. Are you not allowed to do this - 3 levels of swf nesting (child - parent - grandparent)?
UPDATE: Target remote swf's load and display fine directly in Flex as well (skipping AS2 swf). So problem doesn't appear to be with AVM1/AVM2 discrepancy between target content and Flex app. Could be AVM1/AVM2 problem with how the Flex app displays the AS2 swf but only when it contains content.loadMovie("http://URL_TO_REMOTE_SWF"). Loads and displays without that line.
UPDATE: This is a Flex packaged IOS application and thus is subject to ActionScript restrictions for dynamically loaded code. But it doesn't work in a regular Flex (non packaged IOS) app either which is not subject to these limitations. THE FOLLOWING CODE SHOULD WORK AS FAR AS I CAN TELL IN A REGULAR FLEX BROWSER BASED APPLICATION.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="init()">
<fx:Script>
<![CDATA[
import mx.core.UIComponent;
private var request:URLRequest = new URLRequest("[AS2 player swf]");
private var loader:Loader = new Loader();
private var myComponent:UIComponent = new UIComponent;
private function init():void{
myComponent.percentHeight = 100;
myComponent.percentWidth = 100;
loader.load(request);
myComponent.addChild(loader);
player.addElement(myComponent);
}
]]>
</fx:Script>
<s:Group id="player"
height="100%" width="100%"/>
</s:Application>
Since all Sprites and MovieClips are both display objects and display object containers, you could theoretically keep nesting them forever. There are physical restraints, of course, but I see no reason why there should be a limit other than memory and processing power (at some point, refreshing the screen will become sluggish, and eventually painful to wait for).
I've searched the web to see if I could find any information about a built-in limit enforced by the AVM, any documents or error codes that would suggest there is one, but I couldn't find anything at all. Of course, that doesn't mean there isn't - it just means the limit hasn't been reached by many people yet ;)
So if anyone else knows more...
EDIT
Since the information about your loaded MovieClip is new, here's my update: AVM1 (AS1 & 2) SWFs don't always execute in the same way when loaded into an AVM2 (AS3) movie. This has nothing to do with the number of nested display objects. I originally thought this was because the _lockroot property was removed in AS3, but I've searched a bit and found out it is assumed true by default, and should therefore not be a problem.
There may be many things about your scenario that are different from when the file runs on its own, and I would start by looking at these:
Do you use relative path names to load other data into the AS2 swf?
Do you get any error messages (particularly, security sandbox violation errors)?
Does your AS2 SWF rely on external variables to be set (FlashVars)
Does your AS2 SWF need ExternalInterface access?
FINAL EDIT
Since this new info has just come up:
There is absolutely no way to execute dynamically loaded AVM byte code on an iPad at runtime, for the same reason that there is no Java VM and no Flash Player for iOS: Apple does not allow the execution of dynamically loaded byte code at all. Period.
Hence, you can load and play SWF animations, but not ActionScript. It will simply be ignored. Also, neither of the apps that you did run is in fact AVM byte code - they are compiled into LLVM byte code instead.
You will have to find a way to check for the end of the animation that does not involve stop() commands. Perhaps this can be done by checking frame numbers from your AS3 container?
To answer you question directly, yes, you can load AS2 swfs that way. And you code looks correct to me.
I suspect it is loading the AS2 swf fine, but that the AS2 swf is not initializing correctly for some reason. Perhaps it is a video player that has no visible assets besides the loaded video. AS2 tends to fail without throwing errors, which makes it hard to debug. I would suggest publishing an AS2 swf with some graphics in it but with no code that could fail. No code at all. Maybe just a timeline animation of a moving circle or something, then load that. In that way you can eliminate your Flex code as the source of the problem.
If it turns out that your test movie works, then you will need to determine what resource the loaded swf is expecting that it is not receiving.

Issue loading AS2 .swf that uses XML into AS3 .fla

I'm making a website for a client where they purchased a flash template that was written in "AS 1.0 & AS 2.0" and then asked me to add a googlemap to the contact us page. However, the googlemap API only works in AS3. My options were to translate the entire website to AS3 OR to make an AS3 file that imported the AS2 website and then added the googlemap bit.
I decided to go with option 2 since it seemed easier...
Anyway, I'm using the Loader class in the AS3 file.
var myLoader:Loader = new Loader(); // create a new instance of the Loader class
var url:URLRequest = new URLRequest("main_v8.swf"); // in this case both SWFs are in the same folder
myLoader.load(url); // load the SWF file
addChild(myLoader);
The problem is the AS2 swf file uses XML to load the background images. It works on its own but when loaded into the AS3 file the background images do not appear. All the buttons and external links work, its just the loading of the XML.
I've been looking all over for help on this and while there are lots of people with issues playing AS2 swf in AS3 I couldn't find anything to help with this.
Any advise or solutions would be appreciated!
Thanks!
Havent face such a problem but, if you find out the problem you ca solve it. You can check some conditions:
check network for , does xml loaded. (might be crossdomain problem)
check if images loaded. ( as3 security problem)
check as2 file really inits. ( you can use as3 bridge to as2)