Wednesday, May 24, 2006

SmartBitmap -- Caching when you only need it!

Flash 8 introduced one of the greatest features ever, atleast in my opinion, the BitmapData object along with cacheAsBitmap. Using cacheAsBitmap can greatly increase your performance or it can make it much worse. When you use cacheAsBitmap Flash first, renders the vector, then it converts it into a bitmap. It only does this when you're changing a visual property of the movieclip. These properties include the following:

  • _alpha

  • _xscale

  • _yscale

  • _rotation


I still don't know why they (Adobe/Macromedia) didn't include automatic dynamic caching for movieclips. It'd make caching as a bitmap so much more advantagious and general purpose than it is now.

Well, I tackled that problem, with a nifty little class-- SmartBitmap.

A SmartBitmap will only cache when the properties listed above haven't changed for a set period of time (defined when the SmartBitmap is created).
Download:
SmartBitmap.as
Dependencies
Interval.as

FLA Example:
SmartBitmapSource.fla

SWF Example:
SmartBitmapExample.swf
Once the checkbox is unchecked all particles created afterwards are uncached, previous particles are unaffected.


Code Example:


The SmartBitmap class has six arguments:

  • $delay: The time (in milliseconds) to check the above listed properties of change (225 is reccomended).

  • $scope: The MovieClip you wish to attach the SmartBitmap into.

  • $link: The library linkage name of the movieclip that you wish to attach to $scope.

  • $new: The new instance name of the movieclip within $scope.

  • $depth: The depth of the movieclip to be created.

  • $initObj: The object you wish to pass to attachMovie containing predefined properties.




var nDot:SmartBitmap = new SmartBitmap($checkDelay:Number, $scope:MovieClip, $linkage:String, $newName:String, $depth:Number, $initObj:Object);



Once you create a SmartBitmap object like so.


var nDot:SmartBitmap = new SmartBitmap(225, this, "dot", "d"+d, d, {_x:s._xmouse, _y:s._ymouse});


You reference properties directly via the object.


nDot._x += 12;
nDot._alpha -= 14;

nDot.onEnterFrame = function(){
if(random(10) == 0){
this._alpha--;
}
}



I hope someone puts this to good use, if you do comment!

2 Comments:

Anonymous Matt Przybylski said...

Looks real good. I haven't had the chance to really use cacheAsBitmap often in my projects as I'm still doing stuff mostly for 7, but I do plan on doing some 8 stuff for personal clients so I'll give this a shot if I see the need to work around the whole cacheAsBitmap issue. Good work.

Wednesday, May 24, 2006 8:09:00 PM  
Anonymous Matt Przybylski said...

Sorry I keep commenting twice, I always forget something when I go through the first time.

1) you should make a zip file available of all files/dependencies so that we don't have to d/l them all separately.
2) you should keep the folder structure to within one folder, as in when you're calling classes, make it work from that folder rather than me having to go in and edit the paths. i don't have/use flashdevelop and it's kind of a pain to have to go in to all the class files and change all the paths :P

In another one of your files (i think it was the GridPosition class) you used the Delegate class that I think was created by you and was in the flashdevelop/utils dir, which I didn't have, so you should go through all your stuff and make sure you provide it all in the zip and make it easy to just unzip and run without issues. just a thought for lazy people like me :P (but then again if you want people to use your stuff, you should make it as easy as possible)

ultimately, i think you'd get the best use out of your stuff if you included a zip with proper paths and documentation within the zip. so like, make a folder structure of docs/source and put the stuff in the right folders. i normally do docs/source/deploy (deploy is where i publish all my stuff), but i think i'm getting a bit ahead of myself with this whole thing or spewing more stuff than you want to hear :P

anyway, great job.

Wednesday, May 24, 2006 8:13:00 PM  

Post a Comment

<< Home