Actionscript 3 Animated Bitmap Part II

Okay the next version of my Animated Bitmap class is done. I still need a little tweaking but all in all its coming alone 🙂 Below is a test application I created you can see that the Animated Bitmaps all have different frame rates, also these bitmaps can be used to create a button…. go ahead press it 😉

Below is the function call to create one of the Animated Bitmaps:

[code lang=”actionscript”]
var ab1:Object = MemphisAPI.AnimateBitmap({
image:ring_atlas,
width:32,
height:32,
columns:21,
rows:1,
frames:21,
speed:30
});
[/code]

And here is my class, sorry no documentation I’m lazy…. I’ll get around to it thou.
[code lang=”actionscript”]
public static function AnimateBitmap(Properties:Object):Object {
var obj:Object = new Object();
var sprite:Sprite = new Sprite();
var column:Number = 0;
var frame:Number = 0;
var xPos:Number = 0;
var yPos:Number = 0;
var _point:Point = new Point(0, 0);
var _rect:Rectangle;
var sequence:Boolean = new Boolean(false);
var endFrame:Number = new Number(0);
var origData:BitmapData = BitmapData(new Properties.image(0,0));
var newData:BitmapData = new BitmapData(Properties.width,Properties.height,true,0x000000);
var animatedBitmap:Bitmap = new Bitmap(newData);

var timer:Timer = new Timer(Properties.speed);
timer.addEventListener(TimerEvent.TIMER, timerFunc);

initBitmap();

function timerFunc(e:TimerEvent) {
_rect = new Rectangle(xPos, yPos, Properties.width, Properties.height);
newData.copyPixels(origData, _rect, _point);
xPos = column * Properties.width;
if(column == Properties.columns) {
xPos = 0;
yPos += Properties.height;
column = 0;
}
if(frame >= Properties.frames) {
xPos = 0;
yPos = 0;
column = 0;
frame = 0;
}
if (frame == endFrame && sequence == true) { timer.stop(); sequence = false; }
column++;
frame++;
}

function initBitmap() {
_rect = new Rectangle(xPos, yPos, Properties.width, Properties.height);
newData.copyPixels(origData, _rect, _point);
xPos += Properties.width;
sprite.addChild(animatedBitmap);
}

obj.stop = function stop() { timer.stop(); }

obj.start = function start() { timer.start(); }

obj.reset = function reset() {
xPos = 0;
column = 0;
_rect = new Rectangle(xPos, yPos, Properties.width, Properties.height);
newData.copyPixels(origData, _rect, _point);
xPos += Properties.width;
sprite.addChild(animatedBitmap);
timer.reset();
}

obj.gotoAndStop = function gotoAndStop(frame:Number) {
xPos = Properties.width * frame;
column = frame;
_rect = new Rectangle(xPos, yPos, Properties.width, Properties.height);
newData.copyPixels(origData, _rect, _point);
xPos += Properties.width;
timer.stop();
}

obj.gotoAndPlay = function gotoAndPlay(frame:Number) {
xPos = Properties.width * frame;
column = frame;
_rect = new Rectangle(xPos, yPos, Properties.width, Properties.height);
newData.copyPixels(origData, _rect, _point);
xPos += Properties.width;
timer.start();
}

obj.playSequence = function playSequence(startframe:Number , endframe:Number) {
xPos = Properties.width * startframe;
column = startframe;
endFrame = endframe;
sequence = true;
_rect = new Rectangle(xPos, yPos, Properties.width, Properties.height);
newData.copyPixels(origData, _rect, _point);
xPos += Properties.width;
timer.start();
}

if (Properties.buttonmode == true && Properties.buttoncallback != null) {
sprite.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
sprite.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
sprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
sprite.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
function mouseOver(e:MouseEvent) {
obj.gotoAndStop(1);
}
function mouseOut(e:MouseEvent) {
obj.gotoAndStop(0);
}
function mouseDown(e:MouseEvent) {
obj.gotoAndStop(2);
}
function mouseUp(e:MouseEvent) {
obj.gotoAndStop(0);
Properties.buttoncallback(e);
}
} else if (Properties.buttonmode == true && Properties.buttoncallback == null) {
trace(“BUTTON DISABLED: a buttoncallback property is need if using buttonmode = true”);
}

obj.sprite = sprite;
return obj
}
[/code]

Art assets created by:

Next Steps

  • refine the button functionality
  • performance test.
  • refine function call

Now time to get back to work!!

Leave a Reply

Your email address will not be published.