Tag Archives: flash 10

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!!

Actionscript 3 Animated Bitmap Part I

I’ve been working on games for awhile now, and I was looking for a way to make the process of taking art assets and bringing them into flash a little easier.  So my quest started where every quest should start now a days the interwebs!!

What I found was just what I was looking for, a web site called hexagonalstar.com had the info I needed to start creating my own Animated Bitmap class.  So I downloaded there source code and started to work.

I must say that without this source code I would have been stuck, it was very helpful and I just wanted to thank them for all their hard work.

Anyway lets get on to what my plans are.  First, I am slowly created a Flash API code name Memphis, which is a library to help me improve the development time of flash games.  So the animated bitmap class will be added to Memphis as another feature.

What I have right now is pretty simple, just the barebones of the class started.  I can take a bitmap from the library and create the animated bitmap from that image.  What I am planning is to have the ability to start and stop the animated bitmap on command and some other coolio stuff.

Below is my function to create the simple animated bitmap:

[code lang=”actionscript”]
var ab1:Sprite = MemphisAPI.AnimateBitmap({ image:ring_seq, width:32, height:32, columns:21,framerate:30});
[/code]

Which is then passed to Memphis which handles the process:

[code lang=”actionscript”]
public static function AnimateBitmap(Properties:Object):Sprite {
var sprite:Sprite = new Sprite();
var column:Number = 0;
var xPos:Number = 0;
var yPos: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.framerate);
timer.addEventListener(TimerEvent.TIMER, timerFunc);
timer.start();

function timerFunc(e:TimerEvent) {
var _point:Point = new Point(0, 0);
var _rect:Rectangle = new Rectangle(xPos, yPos, 32, 32);
newData.copyPixels(origData, _rect, _point);
xPos += Properties.width;
sprite.addChild(animatedBitmap);
if(column > Properties.columns) {
xPos = 0;
column = 0;
}
column++;
}
return sprite
}
[/code]

So as you can see this is a very basic function right now, but hopefully if everything goes well I will have a fully working animated bitmap class.

Once again I want to thank the website hexagonalstar for pointing me in the right direction 🙂