Speeding up masks AS3 - actionscript-3

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.

Related

Will CacheAsBitmap work on a Movie Clip Object with Frame by Frame Animation?

Hey everyone so I am Using Flash Develop and I have Multiple Move clips added to the stage. Each of course has their own class. Now I have them set up in their class to move across the stage in a Linear motion using a ENTER_FRAME event and coding like so this.x += 10 However these Movie clips each have a frame by frame animation. So I was wondering by adding this.cacheAsBitmap = true; inside the Movie clip objects Class if it would help with the performance or since they have multiple frames if Adobe AIR would have to redraw those frames and cache them as bitmaps hence causing further performance issues. From what Ive read this.cacheAsBitmap = true; will help with performance that way the stage wont have to redraw the movie clip each FPS but has some draw backs like so "You should use cacheAsBitmap only in situation where your vector graphic will remain the same or will have its x or y properties updated."
Any help will be appreciated thanks guys.
You basically got it. cacheAsBitmap won't help (and it will probably hurt) with a frame-by-frame animation.
Using cacheAsBitmap is really only helpful by itself if you are only changing the x,y properties.
Using cacheAsBitmapMatrix (only available in AIR) will let the Flash Player apply transforms (scale, rotation, alpha) with the cached bitmap.
In either case, a frame-by-frame animation is going to force a redraw, which negates the value of both.
As a final note, if you really want to optimize your animation you can try converting it to a sprite sheet.

AS3: Disposing of cacheAsBitmap bitmaps?

I've got a large, detailed, interactive vector object with dynamic text that will frequently translate horizontally from off the screen onto the screen when the user needs to look at it, then back off the screen when the user is done. If I set myVector.cacheAsBitmap = true before translating it and myVector.cacheAsBitmap = false after translating it, what happens to all of those bitmaps that are generated each time? Do I have to dispose of them myself?
Adobe help about Bitmap caching:
Turning on bitmap caching for an animated object that contains complex
vector graphics (such as text or gradients) improves performance.
However, if bitmap caching is enabled on a display object such as a
movie clip that has its timeline playing, you get the opposite result.
On each frame, the runtime must update the cached bitmap and then
redraw it onscreen, which requires many CPU cycles. The bitmap caching
feature is an advantage only when the cached bitmap can be generated
once and then used without the need to update it.
If you turn on bitmap caching for a Sprite object, the object can be
moved without causing the runtime to regenerate the cached bitmap.
Changing the x and y properties of the object does not cause
regeneration. However, any attempt to rotate it, scale it, or change
its alpha value causes the runtime to regenerate the cached bitmap,
and as a result, hurts performance.
Conclusion
If you make a simple translational movement along the x or y axis, your bitmap is created only one time.
Do I have to dispose of them myself?
It seems that you can't touch the bitmap cache only used internally by Flash player.

CacheAsBitmap and filters

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!

Flash CS5...elements are snapping to pixels while moving?

I have an imported png in a 'info_icon' symbol.
I have another imported png in a 'info_content' symbol.
I have some code that allows you to drag an instance of a 'map' symbol around. Inside this map symbol are instances of 'info_icon' and 'info_content'.
When I drag the map slowly, I can see the info_icon instances and info_content instances, jiggle ever so slightly...I think to line up to pixels.
I'm trying to figure out how to stop that. I'm looking at the pixelSnapping property and it doesn't seem to be helping...perhaps I'm using it wrong.
info_button_mc.pixelSnapping = PixelSnapping.NEVER;
Make sure to set smoothing to true on the bitmap.
Try setting scaleX to 0.999 or something like that (in other words, close enough to 1 that you can't tell it's being scaled).
For some reason smoothing doesn't seem to actually get applied unless the scale of the object is something other than 1.
Remember you should be working with bitmaps here, so make sure to export the PNG for ActionScript (right click on PNG->properties) and give it a class name and then:
var bitmap : Bitmap = new Bitmap(new YourPNG(), PixelSnapping.NEVER);
addChild(bitmap);
bitmap.smoothing = true;
bitmap.scaleX = 0.999;
Is info_button_mc a MovieClip or a Bitmap? In addition to what Pixel Elephant suggested, make sure the cacheAsBitmap property is false on the bitmap and its container (if the container is involved with the bitmap's movement). CacheAsBitmap will always pixel snap and it's worth noting that any filters you may have active automatically set cacheAsBitmap to true.
Instead of setting the scale to 0.999 which will result in a blurry image, try rendering your image at double resolution (as in the source image: png, bitmap, whatever - make it twice as big in each dimension as what you actually want in the swf) and then setting the scale to .5. In my experience, this is the best of both worlds. That said, it still may not be perfect enough for text - but it is substantially better quality than setting the scale to 0.999.

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.