class and object in games

Page 2 of 5
CLASS FOR THE SHIP.
OK, now we're looking for the code and explain it.
// ***********************************
// **FONCTION MYSHIP******************
// ***********************************
// we made the var MyShip exist
function MyShip() {
}
// and we define it as a lower class of the MovieClip class. In that case, the class
// MyShip inherit all the methods and properties of its upper class (here MovieClip).
MyShip.prototype = new MovieClip();
// ***********************************
// Now we define the methods of the MyShip class. Here is the method who init the weapon
// of the spaceship.
MyShip.prototype.InitArmes = function(MuniSimple, MuniChercheur, MuniExplosion, MuniTriple, MuniTripleCherche) {
// we've made an 2D array(see MyShip.prototype.Init()). In this one you have to put
// all the name of the MC you've created for your weapon. I.e you have 3 weapons, each
// graph has to be in a different //MC. So you've got 3 MC. I.e simple, explosion and seek.
// The second dimension of the array is for the bullets of each weapon. Ok?
this.MesArmes = [[simple, chercheur, explosion, triple, triplechercheur], [MuniSimple, MuniChercheur, MuniExplosion, MuniTriple, MuniTripleCherche]];
// define wich clip is active. Don't forget that "this" refer to the object who call
// the method.
this.MissileActif = (this.MesArmes[0][0]);
};
// ***********************************
// Here we init the different var use by an object of this class. This method
// have 4 parameter: coords, rotation and speed.
MyShip.prototype.Init = function(U, L, R, W, F, x, y, angle, VitesseMax) {
this.Up = U;
this.Left = L;
this.Right = R;
this.weapon = W;
this.firing = F;
this._x = x;
// abscisse du vaisseau
this._y = y;
// ordonn?e du vaisseau
this._rotation = angle;
// rotation angle in degree
this.MaxSpeed = VitesseMax;
// Max Speed
this.MonAngle = (Math.PI/180*this._rotation);
// rotation angle in radian
this.Vivant = true;
// is my spaceship still there
this.AlreadyRun = false;
// is my spaceship already in accelerate movement
this.Fin = false;
// let me know if a tween motion is ending, i.e the tween of the explosion of the sapceship
this.SpeedX = 0;
// Speed in 0x
this.SpeedY = 0;
// Speed in 0y
this.AngleTourne = 10;
// angle which be soustract or add to the present spaceship's angle. This var'll be call when you push Left or Right key.
this.CheckPointX = this._x;
// X of CheckPoint
this.CheckPointY = this._y;
// Y of CheckPoint
// this two under lines are here because we can tell to flash which
// part of the scenery is the "root" one (in which your spaceShip appears)
this.Ligne = Number(monLevel.Rligne);
// this put the SpaceShip on the correct line
this.Colonne = Number(monLevel.Rcolonne);
// this put the SpaceShip on the correct col
_root.carte.attachmovie("SmallNavette", [this._name+"Small"], 100);
// create an empty MC called youObjectName+Small
// Array with the name of the missile and the number of bullets -1 == infinite
this.MesArmes = new Array();
// mon tableau d'armes
this.MissileActif = "";
// l'arme active
};
// ***********************************
// this method check if the selected weapon always have bullets, if true it return TRUE else FALSE
MyShip.prototype.CheckMunitions = function() {
// we're looking for the selected weapon
for (t=0; t<=(this.MesArmes[0].length-1); t++) {
if ((this.MesArmes[0][t]) == this.MissileActif) {
// we find it so we look if this weapon always have bullets
if ((this.MesArmes[1][t]>0) || (this.MesArmes[1][t] == -1)) {
return (true);
break;
} else {
return (false);
}
}
}
};
// ***********************************
// This method remove 1 bullet to the number of bullet of the selected weapon
MyShip.prototype.EnleveTir = function() {
for (t=0; t<=(this.MesArmes[0].length-1); t++) {
// looking for the selected weapon
if (((this.MesArmes[0][t]) == this.MissileActif) && ((this.MesArmes[1][t])<>-1)) {
// if find and if number is not -1 (which is for infinite bullets), we remove 1 bullets to the number of bullet of the selected weapon
this.MesArmes[1][t] -= 1;
break;
}
}
};
// ***********************************
// this method is called when the player want to change weapon. It looks for the next weapon who have bullets, else select the default weapon (infinite bullets).
MyShip.prototype.ChangeArmes = function() {
var oldposition = -1;
// define the old and new position out of the array
var newposition = -1;
var OK = false;
// ok let us know if we found a weapon
for (t=0; t<=(this.MesArmes[0].length-1); t++) {
// looking for the place of the selected weapon
if ((this.MesArmes[0][t]) == this.MissileActif) {
oldposition = t;
// so we have the old place (the last weapon used)
newposition = oldposition;
// init new position to check the place after
break;
// use to break the script, in that case if the weapon have the first place the script will not look for the other.
}
}
while (OK == false) {
newposition++;
if (newposition>(this.MesArmes[0].length-1)) {
// if we reach the end of the array, we'll come back to the beginning, in that case ALL the place will be check
newposition = 0;
}
if ((this.MesArmes[1][newposition])<>0) {
// if the place we are got a weapon with bullets so we define this weapon as the active weapon
this.MissileActif = (this.MesArmes[0][newposition]);
// now if the weapon use specific graph wich have to follow the spaceship, we'll attach movie to the MC Suiveur (follow in french).
switch (this.MissileActif) {
case simple :
_root.Suiveur.Armes.unloadMovie();
// clear the MC Suiveur
break;
case explosion :
_root.Suiveur.attachmovie("cercle", "Armes", 0);
// attach the graph to the clip Suiveur
}
OK = true;
// we've found a weapon so Ok == true.
}
}
};
// ***********************************
// This method is used to move the SpaceShip object.
MyShip.prototype.deplacement = function(MonDecor) {
if (this.Vivant == true) {
// the spaceShip is still alive.
if (Key.isDown(this.Left)) {
this._rotation -= this.AngleTourne;
}
if (Key.isDown(this.Right)) {
this._rotation += this.AngleTourne;
}
if (Key.isDown(this.Up)) {
if ((this._rotation>=-90) && (this._rotation<=+90)) {
// if my _rotation property is between -90 and 90 then the nose of my space ship is turn to the right so we add the cos to the vector SpeedX (some tuts are about
// vector and Math function. Look about it if you have some troubke understanding how it works.
if (this.SpeedX<this.MaxSpeed) {
// if the XSpeed isn't the MaxSpeed so we add the cos to the X vector
this.SpeedX += Math.cos(this.MonAngle);
}
} else if (this.SpeedX>-this.MaxSpeed) {
// the nose of the spaceShip is turn to the left so we substract the cos to the vector if the cos isn't -MaxSpeed. Here I
// may to use the Math.abs function, but I find better to see the minus sign... for more understanding.
// so I ADD the cos, because the cos is already negative. (look at your old Math lesson).
this.SpeedX += Math.cos(this.MonAngle);
}
if ((this._rotation>=-180) && (this._rotation<=0)) {
// the same thing as above, execpt we work with the sinus
if (this.SpeedY>-this.MaxSpeed) {
this.SpeedY += Math.sin(this.MonAngle);
}
} else if (this.SpeedY<this.MaxSpeed) {
this.SpeedY += Math.sin(this.MonAngle);
}
if (this.AlreadyRun == false) {
// check if my Ship is alredy run else put the state to run
this.gotoAndStop("run");
this.AlreadyRun = true;
}
} else {
if (this.AlreadyRun == true) {
// the player don't press the Up key so the SpaceShip stop and we go to the state STOP
this.gotoAndStop("stop");
this.AlreadyRun = false;
}
}
// set the _x and _y position and the _rotation of the SpaceShip
this._x += this.SpeedX;
this._y += this.SpeedY;
this.MonAngle = (Math.PI/180*this._rotation);
// Moving the following MC, for the specific graph for the selected weapon. i.e for the weapon explosion.
Suiveur._x = this._x;
Suiveur._y = this._y;
// Call function to scroll decor (if necessary). In that case if my spaceShip is one the edge of the scene we made action (that we will see after)
scrollDecor(this, Mondecor);
// This two lines move my small spaceship on the map. The +1 and +2 at the and of the two lines is for coords correction
// I have a pb with that. Don't know how to resolve it so I use this correction.
_root.carte[this._name+"Small"]._x = (60*(this.Colonne-1))+this._x/10+1;
_root.carte[this._name+"Small"]._y = (60*(this.ligne-1))+this._y/10+1;
// check if a wall is where the spaceship is
checkcolision(this, this, MonDecor);
// is the spaceship cross a checkpoint. See the "laoding scenery" part and come again here to understand why
// I do that and hox it works
// I look for the wall and checkpoint which are ONLY on the active part of the scenery
// I use the var maxmur and maxcheck init in the part themselves
for (c=0; c>=MonDecor["L"+this.Ligne+"_"+this.Colonne]["maxcheck_"+this.Ligne+"_"+this.Colonne]; c++) {
if (this.hitTest(Mondecor["L"+this.Ligne+"_"+this.Colonne]["check"+c])) {
Mondecor["L"+this.Ligne+"_"+this.Colonne]["check"+c]._name = ["old"+c];
// my last checkpoint coords are the coords of the last crossed checkpoint
this.CheckPointX = Mondecor["L"+this.Ligne+"_"+this.Colonne]["old"+c]._x;
this.CheckPointY = Mondecor["L"+this.Ligne+"_"+this.Colonne]["old"+c]._y;
// my checkpoint dissappear to become a star
Mondecor["L"+this.Ligne+"_"+this.Colonne]["old"+c].gotoAndStop("effet");
}
}
if (Key.isDown(this.Firing)) {
// the spaceship firing a missile (active missile). Call a method of the class Amissile. We'll see that later.
this.MissileActif.InitMissile(this, MonDecor);
}
if (Key.isDown(this.Weapon)) {
// if the weapon key is press then we change the active weapon.
this.ChangeArmes();
}
} else if (this.Vivant == false && this.Fin == true) {
// if my spaceship is not alive (so the player is dead) and
// the tween of the SpaceShip explosion is ending so I go to the last
// CheckPoint coords and set the speed vector to 0
this._x = this.CheckPointX;
this._y = this.CheckPointY;
this.SpeedX = 0;
// Y speed
this.SpeedY = 0;
// X speed
this.Fin = false;
// end of the anim
this.Vivant = true;
}
};
// ***********************************
// in each frame I call the deplacement method of the object created by the clas MyShip.
MyShip.prototype.onEnterFrame = function() {
this.deplacement(_root.decor);
};
// ***********************************
// to associate a class with a symbol. You have to put a name in the linkage
// area (property of the symbol in the Library Window). Here I put nav. So I
// associate the symbol nav with the clas MyShip. In that case each MC created
// with the nav symbol can use SEPARATLY all the method and properties of
// the class MyShip.
Object.registerClass("nav", MyShip);
NOW YOU CAN HAVE AS MANY INSTANCE (OBJECT CREATED WITH) OF THE CLASS MYSHIP AS YOU WANT. SO YOU CAN HAVE FOR EXAMPLE 2 PLAYERS. YOU ONLY HAVE TO DEFINE THE PARAMETER TO INIT IT. (Up, Left, Right, _x, _y, _rotation, MaxSpeed).
