Documenting a Flash AS2 OOP Game Development

AS2.0 Vector and Enumerator class

These classes are used in Flash MX2004. They mimic the Java Vector and Enumeration classes. The Vector class sort of extends the Array class/type. It makes an array which is dynamically extendable. And provides methods for accessing and searching the Vector.

I have called the class MyVector because I already have a Vector class which is used for 3D vertices.These classes are also fairly similar to the C++ STL templates of Vector and Iterator (??).M The Vector class was adapted from a JVector class in C++ by Michael Olivero from mike95.com.So this class has done a very circuitous route from Java, to C++ and hence to Actionscript 2.0.

Note for Actionscript 1.0 luddites, this and any other class can be very easily adapted to AS1 by just changing the methods to prototypes and the class name to a function. It is that simple. .
Anyway , here is the MyVector class :

class MyVector {
	// mimics Java Vector class
	// and the C++ STL Vector 
	//  Author: Michael Olivero
	//  Email:  mike95@mike95.com
	//  adapted to AS2 by steve happ 
	// 7/3/04
	//	====================================
	// 	------	MEMBER VARIABLES		//
	//	================================
	var m_size:Number;
	var m_capacity:Number; 	// initial capacity
	var m_increment:Number;
	var m_pData:Array;
	//=============================
	//		 constructor
	//=============================
	function MyVector(initialCapacity, capacityIncrement:Number) {
		m_size = 0;
		m_capacity = initialCapacity;
		m_pData = new Array(m_capacity);
		m_increment = capacityIncrement;
	}
	//	-------	CAPACITY	----	//
	function capacity():Number {
		return m_capacity;
	}
	//	-----	CONTAINS	--------//
	function contains(elem):Boolean {
		for (var i = 0; i<m_size; i++) {
			if (m_pData[i] == elem) {
				return true;
			}
		}
		return false;
	}
	//	-------	COPY INTO	------	//
	function copyInto(array:Array):Void {
		for (var i = 0; i<m_size; i++) {
			array[i] = m_pData[i];
		}
	}
	//	-------		ELEMENT AT	----	//
	function elementAt(index:Number) {
		//verifyIndex(index);
		return m_pData[index];
	}
	//	-----	FIRST ELEMENT	---	//
	function firstElement() {
		if (m_size == 0) {
		trace("MyVector.firstElement - array is empty");
		}
		return m_pData[0];
	}
	// ---------- INDEX OF	--------//
	function indexOf(elem):Number {
		for (var i = 0; i<m_size; i++) {
			if (m_pData[i] == elem) {
				return i;
			}
		}
		return -1;
	}
	// -------	IS EMPTY	-------------//
	function isEmpty():Boolean {
		return m_size == 0;
	}
	//	------	LAST ELEMENT	--------//
	function lastElement() {
		if (m_size == 0) {
			trace("MyVector.lastElement - its empty");
		}
		return m_pData[m_size-1];
	}
	//	--------	LAST INDEX OF	------//
	function lastIndexOf(elem):Number {
		//check for empty vector
		if (m_size == 0) {
			return -1;
		}
		var i = m_size;
		do {
			i -= 1;
			if (m_pData[i] == elem) {
				return i;
			}
		} while (i != 0);
		return -1;
	}
	//	----------	SIZE	----------//
	function size():Number {
		return m_size;
	}
	//	------	ADD ELEMENT	--------//
	function addElement(obj) {
		if (m_size == m_capacity) {
			ensureCapacity(m_capacity+m_increment);
		}
		m_pData[m_size++] = obj;
		
	}
	//	-------	ENSURE CAPACITY	------	//
	function ensureCapacity(minCapacity:Number):Void {
		if (minCapacity>m_capacity) {
			var i;
			m_capacity = minCapacity;
			var temp = new Array[m_capacity]();
			//copy all the elements over upto newsize
			for (i=0; i<m_size; i++) {
				temp[i] = m_pData[i];
			}
			delete m_pData;
			m_pData = temp;
		}
	}
	// 	-------	INSERT ELEMENT AT	-------	//
	function insertElementAt(obj, index) {
		if (index == m_size) {
			addElement(obj);
		} else {
			verifyIndex(index);
			//this will throw if true
			if (m_size == m_capacity) {
				ensureCapacity(m_capacity+m_increment);
			}
			var newItem = obj;
			//pointer to new item
			var tmp;
			//temp to hold item to be moved over.
			for (var i = index; i<=m_size; i++) {
				tmp = m_pData[i];
				m_pData[i] = newItem;
				if (i != m_size) {
					newItem = tmp;
				} else {
					break;
				}
			}
			m_size++;
		}
	}
	//	-------	REMOVE ALL ELEMENTS	-------	//
	function removeAllElements() {
		//avoid memory leak
		for (var i = 0; i<m_size; i++) {
			delete m_pData[i];
		}
		m_size = 0;
	}
	function removeElement(obj) {
		for (var i = 0; i<m_size; i++) {
			if (m_pData[i] == obj) {
				removeElementAt(i);
				return true;
			}
		}
		return false;
	}
	//	-------	REMOVE ELEMENT AT	-------	//
	function removeElementAt(index) {
		verifyIndex(index);
		delete m_pData[index];
		for (var i = index+1; i<m_size; i++) {
			m_pData[i-1] = m_pData[i];
		}
		m_pData[i];
		m_size--;
	}
	//	---------	SET ELEMENT AT	-----	//
	function setElementAt(obj, index) {
		verifyIndex(index);
		m_pData[index] = obj;
	}
	// --------	SET SIZE	----------	//
	function setSize(newSize:Number) {
		if (newSize>m_capacity) {
			ensureCapacity(newSize);
		} else if (newSize<m_size) {
			for (var i = newSize; i<m_size; i++) {
				delete m_pData[i];
			}
			m_size = newSize;
		}
	}
	//
	function trimToSize() {
		if (m_size != m_capacity) {
			var temp = new Array[m_size]();
			var i;
			for (i=0; i<m_size; i++) {
				temp[i] = m_pData[i];
			}
			delete m_pData;
			m_pData = temp;
			m_capacity = m_size;
		}
	}
	//	--------	MIN	-------------	//
	function min(left, right:Number):Number {
		return left<right ? left : right;
	}
	//	---------	VERIFY INDEX	------	//
	function verifyIndex(index:Number) {
		if (index>=m_size) {
			trace("MyVector.verifyIndex - is too big");
		}
	}
}

MyVector Usage

Here is an example of how the MyVector class can be used. So here is my test fla.
Try it out and write your own test code…

var m1 =1;
var m2 = 2;
var m3 = 3;
var m4 = 4;
var v1:MyVector = new MyVector(10,10);
trace(v1.capacity());
v1.addElement(m1);
v1.addElement(m2);
v1.addElement(m3);
v1.addElement(m4);
v1.addElement(5);
v1.addElement(6);
v1.addElement(4);
if(v1.contains(m3)) trace("v1 contains m3");
trace(v1.elementAt(2));
var copyto = new Array();
v1.copyInto(copyto);

for(var i=0;i<copyto.length;i++){
	trace(i +" = " + copyto[i]);
}

//	--------	first element	-----	//
trace("First element = "+ v1.firstElement());
trace("index of m3 = " + v1.indexOf(3));
trace(v1.isEmpty());
trace("last element is "+v1.lastElement());
trace("last index of " + v1.lastIndexOf(4));
v1.insertElementAt(0,0);
trace(v1.firstElement()+" = first Element");	
//		remove element at index no.
v1.removeElementAt(7);
trace(v1.lastElement());
v1.setElementAt(7,7);
trace(v1.lastElement());
v1.setSize(6);
trace(v1.size());
trace(v1.lastElement()+" = last ");
trace(v1.min(m2,m3));
v1.verifyIndex(24);
//////////////////////////////
//	Enumerator
/////////////////////////////
var enum = new Enumerator(v1);

trace(enum.getElement(4));
//trace(enum.getNextElement());
//trace(enum.getNextElement());
//trace(enum.getNextElement());

//
while(enum.hasMoreElements()){
	trace(enum.getNextElement());
}

Enumerator class

This is a rip of the Java Enumeration class. It is pretty self explanatory . See the above test code for usage.

class Enumerator {
	// mimics Java's Enumeration class 
	// members
	 var currentElement:Number;
	 var target:MyVector;
	// constructor
	public function Enumerator(ptarget:MyVector) {
		currentElement = 0;
		target = ptarget;
	}
	public function hasMoreElements():Boolean {
		return (currentElement < target.size());
	}
	function getElement(index:Number):Object {
		return target.elementAt(index);
	}
	public function getNextElement() {
		return hasMoreElements() ? 
			getElement(currentElement++) : null;
	}
	
}

Leave a Reply

Your email address will not be published. Required fields are marked *