Efficiency of embedding flash assets in flex actionscript project - apache-flex

I'm creating a project in flex builder but it's not using the flex framework, it's just pure actionscript.
At the moment I have some bitmap resources embedded for drawing things, but I'd like a little animation and thought I could create swf animations for them in flash and use the [Embed()] thing to embed them in the actionscript project.
This works perfectly well for me, however I'm a little concerned about efficiency and how well this approach will scale when my project gets larger.
Does anyone know if there are any efficiency implications to embedding maybe 20 or 30 swf files, or do they get efficiently integrated into the main swf file? Most of the animations are really simple and I could probably redo them in actionscript if needed but it's a lot easier just to draw them in flash and embed them.

Not too sure exactly what you mean by efficiency, but embedding a large number of files is generally not considered best practice. You generally only want to embed things that need to show up immediately when the application loads, or that are so small it would be overkill to load them via URLLoader and the like.
Say you only view 5 of your animated swfs at startup, and the as the user clicks around and goes to different screens/views, they are revealed the rest. Then you could just load the swfs at runtime via URLLoader or the Flex Module architecture.
The benefit of having your swfs and assets not embeddded in your main swf is that:
it dramatically decreases swf size
it makes it easier to swap out graphics at runtime (or without needing to compile)
If I were you I would remove as many [Embed] tags as possible, and replace them with either loading via modules or urlloader.
Best,
Lance

Related

How to reduce SWF filesize by optimizing the code?

Considering we have already done the following actions:
Flex Framework as RSL
Compiling with debug=false
Loading most images at runtime
Drawing other simple images with flash draw features
Reducing complex images with pngquant
Creating modules for secondary features
Applying ranges to fonts
Running FlexPMD to find dead code and bad copy-paste
Running FlashOptimizer and secureSWF (with poor results)
Today our application is 1358k:
Code: 978k - 72%
Images: 270k - 20%
Fonts: 110k - 8%
We believe we spent a lot of time into asset optimization and most of the work is remaining on the code.
By analyzing our link-report, our guess is that the heavy part of the code is comming from Flex .mxml nested components. We don't think there is much to do on our pure AS classes.
Is there any analysis or coding best practice in order to reduce the impact of the code on the swf filesize ?
Thanks.
Here is the application : http://www.pearltrees.com/nicolas/137698/
In my practice I usually don't have big final swf files, so I want to mention only one thing. Using mxmlc directly we should not forget to add (for the final build of course) parameter/attribute
debug = "false"
in other way final swf will be almost 2 times bigger.
do you have an objects in the .mxml that are similar to each other that you could turn into a generic class and customize programatically?
Consider looking into preloaders and modules.
Without knowing your application, it's hard to be specific, but a custom preloader can sometimes help a lot with perceived download time. Let's face it, asking the user to idly stare at a progress bar is sad, and you can do better.
The usual example here is that your need your application users to login, or select some basic details before jumping into the main application. By implementation that first form as a preloader, your application will keep downloading in the background while your user interacts with that form.
The downside: Your preloader code doesn't have access to all the Flex goodness. You'll have to draw your UI and implement your interaction in plain old AS3. Still, the extra work can be worth it in some situations.
Flex Modules are the other thing that'd be worth looking into. In a complex Flex app, not everything is commonly used. If you cut the lesser-used bits from the main application and move them into a module you load on-demand, you may be able to save a fair amount of bytes from the initial download size.

Disadvantages of a Flex project vs an Actionscript project?

I've recently started making a game in FlexBuilder. The game is currently a Flex project.
Is there any downside to using Flex as opposed to just Actionscript?
A friend of a friend told me that Flex is slower than an Actionscript project. I've been unable to validate this on the internet; is there any truth to that claim?
Thanks!
If you're developing a game, you should be using an ActionScript project. Flex is to be used only for data driven applications and user interfaces.
The flex compiler generates a lot of intermediate code to convert mxml files into actionscript (you can view those files if you compile with -keep compiler option). This code, the flex framework incorporated to your SWF, adds significantly to the size.
Create an actionscript project with a textfield ("hello world") and a flex project with a label with the same text. Build them, go to their bin-debug folders and compare the file sizes. While actionscript one is only a couple of kilobytes, flex swf would be at least a 100 kB.
As far as speed is concerned, since the flex framework is sitting on top of actionscript, it obviously would have a performance downside.
The beauty of flex lies in the easiness to create UI components and developing data driven applications that frequently communicates with the server.
Don't use it unless you truly need it.
If you know flex and it helps you developing faster - do it in flex.
Download size doesn't matter for game with a lot of assets. Most popular flash games have size of 5 MB and more. (for example on kongregate)
Crucial part of game you can make in pure actionscript. Placing hundreds UIComponents to Canvas could doesn't slow in Flex, but Flex rending technology prevent smooth animation of objects.
Conclusion:
Flex is for rapid development. You can use it's easy skinnable components for menu. Even without Flex UI components binding is matter and makes life easier.
Download size = speed on the Internet. The smaller you can make your game, the faster it will load. Size also equals speed in an interpreted language like ActionScript, where the less code you have to execute, the faster it will run. Hand-coding an ActionScript routine might let you make it faster than the generalist approach of Flex.
That said, maybe you'd be willing to pay that overhead to avoid having to write a lot of utility classes in pure ActionScript. Your high-score screen would be easier to do in Flex, for example, and that might be worth the overhead to you. It won't matter if your high-score screen is a little slower, since it isn't a real-time thing like the actual game.
Also consider the cost of your time. By using Flex, you'll be done with those parts of your app faster than if you hand-code them in ActionScript. Unless your time is free, you should be thinking about how this trades off against the bandwidth cost of the Flex overhead. It might be that it's cheaper to pay the bandwidth than your time making a more efficient program.
A Flex Application won't necessarily run slower than a Pure AS3 application once it's fully loaded - everything gets compiled down to bytecode in the end, and a Flex App is like an AS3 app that uses a LOT of other classes.
Think of the Flex Framework as a set of shortcuts that allow you to do things much more quickly, but the real cost is that your project gets filled up with a LOT more code - even if you're actually writing less code, and you never have to see the additional code.
I would disagree with the assertion that Flex should only ever be used for complex data-driven applications, though it's certainly very useful for such projects. You can use it for anything, as long as you understand that the final product (the .swf you export) is going to be a lot larger than it might otherwise be.
If having a large .swf is not that big a deal to you (and it might not be, depending on what you're doing with it) then I'd say give Flex a try because ArrayCollection, RemoteObject and Data-Binding will save you hours of frustration and hundreds of lines of code.
However, if you want to make sure your final app is as small and efficient as possible, do it in pure AS3 and simply opt-in to more advanced libraries as you need them.
Flex provides a framework for building Rich Internet Applications. If your game requires a complicated GUI (such as an RPG), it might be useful. Otherwise, it adds layers that will complicate things if all you want to do is build a game.
As said above in the first point the only beverage is memory consume. But if u write in pure AS script you need to be in deadline combat. As flex provide lots of customizable comps. U can use Class files instead of MX Comps that save the Memory. Do not Create any components unless it is being used. Modules and RSLs are avail to acheive the peretainity

Alternative to Flex Framework

Is there an alternative ActionScript 3 lightweight framework out there similar to Flex, but not as huge. Flex is fairly large, and SWF's being no less than 150Kb big, I'm just thinking it must be possible to get something that have smaller SWF files as an end result.
I've come accross AS Wing, and was curious if there's more.
Thanks for the replies in advance!
This may be what you're looking for :
http://code.google.com/p/flit/
I haven't tested it yet so I can't really say if it's worth it or not
You can change some settings when compiling to reduce the size of your .swf. Here is a blog posting showing how.: Reduce Flex 3 File Size. Basically you compile the sdk into a separate .swf that gets cached by the flash player.
http://www.openlaszlo.org/ is an open source alternative that can generate swf8, dhtml, or swf9.
The GraniteDS framework provides a clean-room re-implementation of the MXML->SWF compiler, and it may possibly give you options to generate smaller SWF files. It can also do that compilation on the fly, which is pretty cool.
Why don't just use Flash? You can dump your logic in AS class file.
Swf from Flash can be pretty small, can down to 8kb, or even smaller.
Correct me if I am wrong, I see Flex as a Flash with XML Layout Manager (which can position components well during resizing). If you are willing to spend time on designing a Layout Manager for Flash, i think Flash is a good choice. Flash supports AS3, pureMVC.
Of course, Flex is free :)
check out http://www.weaverfx.com/index.php?option=com_content&view=article&id=13&Itemid=6
they claim that applications built with there framework are of the tune of 20 to 100Kb
AS3Commons UI Invalidation & LifeCycle provides an entire component life cycle framework similiar to that of Flex but written in pure AS3 an hence small and independent. If you are about to create your own components you should give this library a try.
Edit: self-plug
Another component libary:
AS Data Provider Controls is a new set of standard UI components built with plain ActionScript. Open source.
Edit: self-plug
Thanks for all the helpful replies. I'm busy experimenting a bit with an ActionScript 3 project in Flex Builder, and I think I might just create the components I need myself and make them modular for re-use in other projects.
That way, I can include exactly and only that what my application is using, and not worry about an additional x00000Kb of data that gets dragged along either as an RSL, or merged in my code.

GWT style code splitting in Flex

I do a lot of work with GWT but don't have experience of Flex. I was talking to a guy today who was looking at moving some large Flex based applications to GWT due to the Flex application getting too big and using too much memory in the browser. This is a problem I have had before with GWT - browser apps using lots of memory as all the code gets loaded when it starts.
However, in GWT 2.0 there is now a code splitting feature to overcome the problem of the client code getting too big. This allows all the code (javascript) not to be loaded as one big file on start up but instead code split into different files that can be loaded when required.
I was thinking as to if there is anything similar in Flex. I assume the Flex application code all lives in one single SWF file which loads at start-up so this approach is not possible but thought there might be other solutions.
You might want to look into Modules. Otherwise the code is in one SWF. You can also move the Flex framework code out of yur SWF. And you can always dynamically load your resources such as images.

When should an oldschool flash developer use flex?

What are the key differences between Flash and Flex? I have over five years experience with flash and feel very comfortable developing with it and ActionScript3. I find myself more and more curious about Flex and want to know when it is best to use flash or flex. Also, is everything that can be done with MXML, able to be done with AS3? I have a strong understanding of AS3 and OOP and would like to know the diffrences between using AS3 and MXML in Flex.
Flex is great if you quickly want to build a UI, you can mock up a functioning UI in a couple hours. Since it still can be limiting for some custom UI's it's not perfect for everything but if something should "look" more or less like an application and fit in a grid it's super quick to mock up the UI in MXML. Also don't be intimidated of how most Flex apps look (ugly, imo), you can customize everything or easily create your own components.
Putting actionscript in mxml is the same as putting css or javascript in html = really bad. Unfortunately even Adobe has this in multiple examples (probably mostly because it's easier & faster for demostrations).. My personal opinion is that this applies to bindings too, as i don't want to put my data in the UI (mxml).
As an experienced developer I'm sure you don't do any development on the timeline (to clarify the Flash = timeline misconception). Still with Flex you have the UI separated in a framework that handles a lot of the burden with layout so that you can concentrate on the business logic. The rest of the workflow is close to what you probably already have with Flash.
It depends on what kind of applications you are developing now with Flash. I have been a Flash developer (mainly applications) for 7 years. I must honestly say that I was extremely glad when Flex 2 was released because it had the component framework (good components, layout managers, ...) I did not have in Flash. This is IMO the biggest difference between Flash and Flex (or the Flex framework).
MXML is a real blessing, especially when using data binding. In the end, everything is compiled down to ActionScript (check the -keep compiler option), but MXML just saves you so much time.
Flash and Flex provide different ways to produce different things. I am not familiar with Flash, but I would expect that it is dependent on a time-oriented way to produce something, whereas Flex is geared toward more traditional software development. That is, rather than dealing with time and frames in Flash, one is dealing with describing where components should be placed with MXML and how those components work with ActionScript.
One should also be able to write a Flex app with just AS3 and no need MXML.
The main difference between AS3 and MXML in Flex, as far as I know, is that MXML is not intended to be used with application logic, but rather it is intended to be used like HTML/CSS in web pages and puts components and content onto the Flex app. ActionScript is used to program behaviors, components, and other things outside or what MXML does. Thus, if you want to attach an event to a component one would write ActionScript code.
Hope that helps. I am still learning about Flex myself.
Some other differences that come to mind:
Flash allows you to create graphical assets and then work with them immediately. To use those same things in Flex, you need to use Flash to export them to a swf or swc first.
Flex has a layout manager, so applications that have variable window size are waaaay easier to make. For instance, you can take a window and set it to 90% width of the window, and it will change size... not scale mind you, but actually change its width as the window is made larger or smaller. This is not easy outside of the Flex framework.
Data Binding in Flex is a huge timesaver. It essentially creates all of the code you'd need to write in AS3 by simply saying blah="{foo}" The curley braces denote "bind to this".
The Flex Debugger is vastly superior to the Flash one. There is also a Profiler.
Since I started with Flex and not Flash, I'm not sure what kind of IDE is best for Flash dev, but the Eclipse based Flex Builder is quite nice. The code hinting is great. Subclipse integration is great.
Really, Flash and Flex are different beasts. You should know and understand AS3 if you want to use Flex, and since you do, you're in a perfect position to take advantage of Flex's features. Flash is not going anywhere as a tool for making more visually creative pieces, but Flex offers a lot of advantages for application development.
I prefer Flash IDE vs Flex (aka Flex Builder aka Flash Builder for my comment)
In general i would say it depends on the size of the project.
I find it easier to start and finish small projects quickly in Flash.
I would advise Flex for larger projects because it has various debug tools that can save you plenty of time (although i would still just use Flash my self)
But maybe if you really get used to flex, that might not matter.
some Cons of Flex from my experience.
When working on a team of 4 on a
large project, Flex failed to keep
the project
settings from one computer to another. (we shared files using SVN)
Flex constantly conflicted with SVN for us.
I felt distant from the art assets.
some Pros of Flex
being able to follow variable references from one class to another at the click of a button.
being able to easily see many variables while debugging. w/o needing to trace them.
and Flash used to not have Custom Class Code hinting, but now with CS5 it does.
I think you can use the newest features of Flash Player w/o waiting for a new Flash CS#, for example MoleHill (a new 3d api that uses the GPU) has a beta release out right now. and i think the Flex SDK can already use it.
hope this helps.
it should be noted that I am a rare case that doesn't prefer flex, most people strongly prefer flex, so you should give it a try at least.
MXML compiles to action script so it's really like a higher level version of that. So, yes, everything that can be done with MXML can be done with actionscript (but not the other way around).
Flash CSx:
GUI\Layout: Basic GUI class framework
Graphical Content: Great for editing graphical library objects with or without animation
Code: Lacks a good code editor
Flex/Flash Builder + Flex Framework:
GUI\Layout: Advanced GUI class framework and layout engine (Flex)
Graphical Content: Lacks drawing capabilities of Flash, but you can include Flash generated graphics by exporting them for ActionScript into a SWC and importing/referencing the SWC in Flash Builder.
Code: Much better code editor than Flash; not sure if it's on par with FlashDevelop
Other: Supports MXML, which is basically just another style of laying out content. Instead of writing a bunch of "c = new C()", "c.prop = x", "c.addChild"... you can structure display objects and thier children using XML constructs, and the MXML compiler will convert it all back into the less-readable, but basically the same AS3 code.
These technologies are all related and interoperable. They are natural and predictable extensions of the Flash player and ActionScript techonolgies, but for some reason Adobe developed the Flex/Flex-builder/MXML technologies as a totally separate product, and market it as something totally new and oh-so-amazing. Whatever. So now we have to go back and forth between the two to use all the features, which is LAME. They also have to waste time and resources developing unnecessary, but helpful, packages like the "Flex Component Kit" to reduce the number of steps necessary to get Flash content into Flash Builder.
You have to go back and forth between these applications, because of their mutually exclusive features -- Flash Builder lacks graphics editing, and Flash CSx lacks MXML and a good code editor -- but they're interoperable in the sense that you can use Flex classes in Flash, Flash classes (and their embedded graphics) in Flex, you can use Flash Builder and MXML without Flex, etc.
I think they need a single, truly integrated Flash IDE, so they need to merge Flash Builder into the Flash CSx editor.

Resources