Pretty much.
At this point, you need to “abstract” the functionality of your basic mech class so you can create an object.
Say for instance each one has a velocity, speed, direction, size, etc. You can create variables to hold all of those values internally, which means that each mech object will have its own velocity, speed, direction, size, and so on. So, no more arrays, no more mess. Creating custom objects really is about organization, and making things easier for the programmer to design and conceptualize what each object needs to do. It also keeps your code from getting really messy, since everything is tidied up into separate classes. That way, the functionality that is related can be grouped together, and vice-versa, the functionality that is not related to a class, can be kept outside, or in another class.
Ok I’ll write a simple mech class here and show you how easy it is (this will take me like two or three minutes):
class mech extends MovieClip {
static var nConversion:Number;
private var nSpeed:Number;
private var nSize:Number;
private var nRadians:Number;
private var vx:Number;
private var vy:Number;
public function mech() {
nConversion = Math.PI / 180;
}
public function moveForward() {
doVelocity();
//using the 'this' keyword...
this._x += vx;
this._y += vy;
}
public function moveBackward() {
doVelocity();
//...is the same as not using it
_x -= vx;
_y -= vy;
}
private function doVelocity() {
nRadians = this._rotation * nConversion;
vx = Math.cos(nRadians) * nSpeed;
vy = Math.sin(nRadians) * nSpeed;
}
}
Then, back in your main code, you will probably use an Array to keep track of all the mechs on stage, but you will now only need to keep track of the objects themselves, and not manage huge arrays of other data. Assuming you export a MovieClip (the graphical element) from your library with the identifier “alphaMech”, and export it as the mech class, you can write:
var allMechs:Array = new Array();
//create mechs
for(var i:Number = 0; i < numberOfMechs; i++) {
var newMech:mech = attachMovie("alphaMech", "newMech", this.getNextHighestDepth(),
{nSpeed:10, nSize:20});
allMechs.push(newMech);
}
selectedMech = 0;
//key left
this.onEnterFrame = function() {
if(Key.isDown(Key.LEFT)) {
allMechs[selectedMech]._rotation -= 5;
} else if(Key.isDown(Key.RIGHT)) {
allMechs[selectedMech]._rotation += 5;
}
if(Key.isDown(Key.UP)) {
allMechs[selectedMech].moveForward();
} else if(Key.isDown(Key.DOWN)) {
allMechs[selectedMech].moveBackward();
}
}
Something like that. You can take that code and play around with it, and find something that works for you.
Incidentally, I have a tutorials section on my Web site, but I am too lazy to update it most of the time (http://www.indieflasharcade.com). Check it out, and I think I may add this as an example of how to write a simple class ;)
-Indie