AS 2 Classes

9 posts

Flag Post

hey, ive looked in the sticky and have googled it, but cant find a good tutorial on how to create classes. Ive worked with C++ on classes alittle and Ive been told that they can make games run much smoother when on a browser. so what code do i need in my .fla file to access say, ball.as??
im guessing something like:


var my_ball:ball = new ball();

or something like that. Not really sure. and if anyone has any good tutorials they know of PLEASE let me know

Thanks in advanced!

 
Flag Post

Yeah that code would work if you had a ball class defined in ball.as. The as file could look something like:

class ball extends MovieClip {

  private var myVar:String;

  public function ball() {
    //constructor
  }

}
 
Flag Post

ok thanks alot, but i still dont get how using a class can actually help???

 
Flag Post

It won’t help unless you use it properly, but that’s not too difficult to imagine a scenario where you might need them.

Ok, so say your ball needs to have a few variables (properties), like velocity:

private var vx:Number;
private var vy:Number;

Maybe the ball is supposed to move across the stage, so you write a function that will move the ball:

public function moveBall() {
  this._x += vx;
  this._y += vy;
}

So now, imagine that you want to have 10 balls on the stage at once. Instead of writing some complex logic to keep track of all the balls and their velocities, and a function to move them all, you only need to use one command on each ball: moveBall(); That way, you can “encapsulate” all the funcionality of the ball into a single class, and create as many balls as you need.

Also you can use inheritance to create different types of ball, each building on the basic ball class, simply by extending the class. That way if you need three different types of ball that all have the same basic functionality, you don’t have to re-write the code for each one— just extend the class and add on the additional functionality to the new class.

 
Flag Post

OH, i think i get it!!

So I have created a game thats like a mechAssualt based game and I have enemies that move at all different speeds and sizes and stuff, but right now (im probably gonna sound like a noob for doing this but) im using an Array to keep track of every different enemy speed and size, ect. and all this seems like A LOT of code and space, so using a class can keep all that away?? I think thats about what your saying right?

 
Flag Post

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

 
Flag Post

Incidentally, I have a tutorials section on my Web site, but I am too lazy to update it most of the time

What about

I am always making new and better tutorials, so check back to this page periodically for the latest installments.

 
Flag Post

Well, I did say “periodically”…. :)

 
Flag Post

hey, thanks a lot indie!! that helped a ton.