as3 cache as bitmap listener? - actionscript-3

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/

Related

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.

How to implement camera without moving anything AS3?

I'm using physInjector and therefore cannot move clips containing my objects: The physical engine works incorrectly because of it.
I think about implementing something like a bitmap drawing a selected part of the stage on itself. How can it be done? I've read this Trying to capture stage area using BitmapData, but there the author copies its data from the stage, whereas I need the area outside it.
Besides, aren't there less resource-consuming solutions?
First, you may want to use a global Sprite to put your clips insde, like :
var a:Sprite = new Sprite();
a.addChild(myClip1);
a.addChild(myClip2);
...
Then, you should be able to move a.
If you don't, and your physic engine rely on Stage to work, you should probably try to fix it, or to understand better how it work so you can move your movieclips.
Redraw a BitmapData every frame will require a lot of CPU ressource, and you won't be able to interact with your clips. That's really not the best way to go.
x=-(player.mc.x-stage.stageWidth/2);
y=-(player.mc.y-stage.stageHeight/2);
if(canvas)
removeChild(canvas);
var bd:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,false,0xFFFFFF);
bd.draw(stage);
trace(bd);
canvas=new Bitmap(bd);
addChild(canvas);
x=0;
y=0;
At PC it works fine. Don't know whether it is suitable for mobiles.
I also haven't tested the approach suggested by blue112 because in twitter of physinjector developer there are complaints about ANY moving of parent clip (https://twitter.com/reyco1/status/327107695670853632) and it is quite difficult to combine with my existing architecture.
Changing the globalOffsetX and globalOffsetY properties also didn't help

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!

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.