CacheAsBitmap and filters - actionscript-3

Why is cacheAsBitmap automatically set to true when using filters?
I have a movieclip that is drawn to the stage every frame and since it has a playing timeline and it rotates every once in a while I want to have cacheAsBitmap set to false. But the movieclip has a dropshadow-filter which sets cacheAsBitmap to true, doesn't this mean that I lose a lot of performance by caching every frame?
Would removing the dropshadow-filter from the movieclip being drawn and apply it to all its children instead be a solution?

From the Docs:
The cacheAsBitmap property is automatically set to true whenever you
apply a filter to a movie clip (when its filter array is not empty).
If a movie clip has a filter applied to it, cacheAsBitmap is reported
as true for that movie clip, even if you set the property to false. If
you clear all filters for a movie clip, the cacheAsBitmap setting
changes to what it was last set to.
I believe this is because Flash Player has to use the bitmap version of your MovieClip to apply the effects - most filters are done at the pixel level. It is then cached so further transformations (like x/y) are more efficient. So really it doesn't really make a difference wether cacheAsBitmap is set to true or false.
Using filters + timeline animations is generally a bad idea. However, a good optimization would be to apply the filters deeper inside the display list, on objects that are not animated. Even better, try to use bitmap with rasterized effects if possible so your effects are not calculated at runtime.
Don't forget to use Scout to see exactly what the player does to your objects!

Related

Speeding up masks AS3

I'm currently doing the following when applying a mask to a MovieClip:
mc1.cacheAsBitmap = true;
_mask.cacheAsBitmap = true;
mc1.mask = _mask;
Which works great, however...
mc1 is a complex vector animation, and cacheing it as a bitmap in order to mask it has pretty big memory implications from what I can see, and have read.
Is their another way to implement masks? Or a way to optimise the usual solution?
Thanks
edit
Both the mask and mc1 are MovieClips, and they have been added to the stage, the mask is a gradient.
I am using Flash CS6, both movieclip and mask are added to the timeline, where they are being animated
You can use http://www.greensock.com/blitmask/
Quote from the documentation:
Can’t I just set the target DisplayObject’s cacheAsBitmap property to true and get the same result? Why use BlitMask?
If you set a DisplayObject’s cacheAsBitmap property to true, Flash takes a bitmap capture of that object so that when you move it (only
altering the x and/or y properties), the text and vectors don’t need
to be re-rasterized again before being rendered to the screen.
However, Flash would still need to concern itself with extra pixels on
every frame if you’re masking them to only show a small portion of the
area. BlitMask, however, only cares about that smaller masked area
(after the initial capture of course) which alleviates Flash from
having to even think about the extra pixels.

Flash memory management and Actionscript

There was many discussions about this problem, but I want to pay attention on the situations that IMHO seems not so clear:
Yes the general rules are:
Remove chachedAsBitmap
Stop movieClip if playing
Remove events
Delete references
etc.
But let's look:
First Example:
I have nested sprite (ex: mainSprite), it contains other sprites with dynamic textFields in it (and are chached as bitmaps), just textFileds and MovieClips with event listeners on it (with weak reference).
When I need to remove this sprite I need first to remove all it's nested content via loops or just
removeChild(mainSprite);
mainSprite=null;
is just enough?
Second Example:
I have some sprite in which I'm loading bitmap and manipulating with bitmapData, later I'm just replacing content of this sprite with another bitmap, is allocated memory for older bitmap automatically erases and is overwritten or it still exists?
Third example:
I have some "graphics template" MovieClip (in library with Export for Actionscript property set on it) which I'm adding on the stage and filling with dynamic data (and adding event listeners), let's say that it's one scene of the app, on another scene I need same MovieClip with other dynamic data, but inbetween need to clear my stage (need something like transition animation which is also library MovieClip), what's the best way: to set this MovieClip visible property to false (while transition animation is plays) and then reuse it, or just remove it with removeChild and then add when add with addChild once more?
All I wrote is more about Air Mobile, cause in most cases for the desktop these situations aren't so problematic, but in case of mobile development they are.
You can visually monitor memory usage along with fps etc using this lib: http://code.google.com/p/flash-console/
hope that helps.
P.S. gc in flash is always a weird thing :)
First example: removing mainSprite from display list is enough if there are only weak listeners on its children.
Second example: I'd advice reusing the same object with visible = false. Recreating the same object is more resource expensive plus you get another instance of the same thing being in memory before it gets gc'ed.

as3 cache as bitmap listener?

If I run cacheAsBitmap = true on a complex Sprite the caching can sometimes take up to a second.
Is there any way to check for when a DisplayObject has finished being cached?
Nope, there is no possible way. However, cacheAsBitmap is using a system of creating a Bitmap object under the hood. This bitmap gets drawn with the pixel information of the DisplayObject. Hence, if you desire a fine-grain control over how cacheAsBitmap works, I would say that you should instead use Bitmap/BitmapData directly, instead of using a Sprite/MovieClip with the cacheAsBitmap feature. Hope this helps!
This article should help you if you do not know how to work with Bitmaps:
http://www.flashgameblog.at/blog-post/2010/04/08/blitting-the-art-of-fast-pixel-drawing/

AS3 Animation with Bitmaps

I'm doing an animation class in ActionScript 3 and would like to know the most efficient way to do it.
Currently what I do is get an image (sprite sheet) and keep all the frames in an array of Bitmaps, then add each frame as a child and I put setVisible = false except the frame I have to show.
The other way I can think of is to have only one Bitmap added as a child and every time frame has to be changed, I copy the pixels to the Bitmap using copyPixels function.
There is somewhat more efficient than either of these alternatives?
Thanks
Keep all your frames in a vector of BitmapData. Then use a single Bitmap and change it's bitmapData property when you want to change frames.
Copypixels is quite fast.
There's an excellent comparison of rendering methods here at 8bitrocket.com; I would suggest giving it a read.

Dealing with display objects on the stage

I think there are 2 different practices with dealing with display object on the stage.
You draw all the objects on the stage and then manipulate the "visible" attribute to show or hide them.
You use addChild and removeChild to manipulate the visiblity
How do you think what is the best method for this?
Chris
I think the visible property should be used, you don't have to keep track of the display object visibility, unlike removeChild, which will throw and exception if you call it in a DisplayObjectContainer that does not contain a child.
You use addChild and removeChild to manipulate the visiblity.
It has some advantages comparing with timeline movieClip
1. Effective drawing,
2. flexibility in handling,
3. Increased performance and
3. granularity of its application programming interface (API)
I think it chages under conditions, if you won't use movieClip again you can use removeChild, and as i know visibility change dont stop its timeline. If you dont need to track for childIndexs you can remove from stage.