Showing posts with label actionscript 3. Show all posts
Showing posts with label actionscript 3. Show all posts

Sunday, November 30, 2008

Bitmapdata .disose()

This is just some venting...I am working a fraction game..i've tentatively called "Fraction Wars"..basically you blast up equivalent fractions. Anyway, I'm using a fair amount of blitting ala 8 bit rocket style. I decide to create and cache some text animations that use this blitting technique. The logic is that I would cache all the text animations that were likely to occur during the more intense parts of the game and then just blit them to the main canvas, a single bitmapdata object that spans most of the screen. (for in between levels when not much action was occuring, I used regular old text fields and transitionmanagers).

The morale of my wasted time is that bitmapdata's dispose() method is DESTRUCTIVE. Only use it if you don't need ANY reference that given bitmapdata. I knew this before today , just didn't see how it



I wasted more time than I care to share over the following embarrising fact that I had a timer loop that

1) created new bitmapdata by drawing from a text field
2) store bitmapdata in a slot in an array
3) before moving on to the next iteration of the timer, I called .dispose() on the new bitmapdata

I know it seems obviuos now

Friday, November 28, 2008

Randomize Elements in an Array

I had to rewrite some code that randomized elements in the new Vector class (had written the code below to randomize array elements)and thought I'd post both the old array randomizer() here. nothing fancy at all...just thought might be useful to someone.


var myArray:Array =[1,2,3,4,5,6];

function randomize(arr:Array):Array{
var originalA:Array = arr;
var newA:Array=[];

while(originalA.length > 0){
var index:int = int(Math.random() * originalA.length);
var element = originalA.splice(index,1);
//splice returns an array.
newA.push(element[0]);

}

return newA;

}

trace(randomize(myArray));


Note: the splice() method of the array class actually returns an Array made up of the elements you've spliced out of the original array. This is why we need to push element[0] into the new randomized array.

Saturday, November 22, 2008

Loading External BitmapData into Vector of Type BitmapData

I'm working on a math game that requires many external images to be loaded into Actionscript 3's new Vector class (typed array) . It took me a bit of experimenting to determine how to properly load external images into a Vector of type BitmapData. Before the new the typed arrays of our new Vector class, I didn't have to worry about the type of the data that I was storing (arrays can handle any type)...now, it's a whole new ball game

. In the end, you just cast the content of the loader object to a Bitmap then access that Bitmap's ".bitmapData" property...see a simplified version of my code below.




//Here's my BitmapData Vector
var explosionVec:Vector. = new Vector.();

//load external png
var _loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded,false,0, true);
_loader.load( new URLRequest("bath/To/Image/explosion.png"));

//
function loaded(e:Event):void{
//cast content to bitmap
var bmp:Bitmap = Bitmap(_loader.content);
//store the bitmapData from the Bitmap!
explosionVec[0] = bmp.bitmapData;

}

Wednesday, November 19, 2008

Vectors! Typed arrays come to Actionscript 3

Finally got my hands on CS4... one of the main new new perks of the Actionscript is the new Vector class (nothing to do with physics or vector graphics)...these are good old typed arrays in the style of C++ or Java.

Basics of the Syntax

//create a Vector and call it vec

var vec:Vector. <int> = new Vector.<int>();

now "vec" can only store ints! This allows for Type-safety in your code, and potentially performance gains.

to add an object to a vector just call .push()
//the next line adds the int '3' to the vector
vec.push(3);



Much more info over at Senocular