Sunday, March 13, 2011

Beginning Flash and ActionScript Game Programming Part 7: Basic Programming Concepts-Classes and Objects

Beginning Flash and ActionScript Game Programming Part 7: Basic Programming Concepts-Classes and Objects:

In the previous section, we learned about functions, which allows us to create blocks of re-usable code that creates cleaner code, as well as saves time typing.

In this section, we'll get into the "meat and potatoes" of programming– creating classes and objects.

Setting Up FlashDevelop

Open FlashDevelop, Click Project->New Project, select "AS3 Project", name it "part7"(or whatever you want, really), select a location to create the project, and click "OK".

On the right side, in the window "Project", double click on the "src" folder, and double click on the file "Main.as" below it. Now you have a base Actionscript 3.0 progam setup in FlashDevelop.

Creating a Class

Creating a class is simmilar to creating a function in structure, but each class is required to be in its own individual file.

To do this in FlashDevelop, on the right hand side of the screen, right click on the folder "src", select "Add" then "New Class".

All you need to do here right now is name your new class, so beside "Name" change it to "Enemy" and click "OK".

You should now have a file that looks like this:

Enemy.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  
{
 /**
  * ...
  * @author Chris Moeller
  */
 public class Enemy 
 {
 
  public function Enemy() 
  {
 
  }
 
 }
 
}

What is nice here is that FlashDevelop sets up the "skeleton" of the file for you.

Line 1: This is the "package name", which you can also think of as the folder name. Here, this file is just in the main "src" file so it has no package name, but if you had created it in a folder called "BadGuys", the this line would look like: "package BadGuys".

Line 3–6: This is an automatically generated comment, which you can erase if you want to, or provide information about what this class will do.

Line 7: This is where you define the name of your class. This needs to to be the same name as the file.

Line 10: This is your class's "Constructor" function. A constructor is named the same as your class, and is called automatically whenever a new object of this class type is created.

Now that we have made our first class, lets make it do something!

Enemy.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package  
{
 /**
  * ...
  * @author Chris Moeller
  */
 public class Enemy 
 {
  public var name:String;  
  public var type:String;
 
  private var total_health:int;
 
  public static var total_enemies:int;
 
  public function Enemy(name:String, total_health:int, type:String) 
  {
   this.name = name;
   this.total_health = total_health;
   this.type = type;
   total_enemies++;
 
   trace("New enemy created, total is now: " + total_enemies +
   ", name = "+name+", total_health = "+total_health+", of type = "+type);
  }
 
  public function TakeDamage(amount:int):void
  {
   if(total_health<=0)
   {
    trace(name+": Stop beating my corpse!");
    return;
   }
 
   total_health-=amount;
   if(total_health>0)
    trace(name+": ouch, you damaged me!(health:"+total_health+")");
   else
    trace(name+": Eck, you killed me!");
  }
 
 }
 
}

Line 9–14: We create a few variables that we want for each of our "Enemy" objects.
We'll create a "name" and a "type" public variables (variables that can be seen outside of this class), a private "health" variable, that can only be seen from inside this class, and a static variable, which is a variable that is stored for the entire class, instead of for each object created.

Line 16: Our constructor, which is automatically called when we create an object of this type. Here, all 3 variables are required to be passed in when creatng an object of this type– it must be given a "name", an amount for "total_health", and a "type".

Line 18–20: These 3 lines are used to assign the passed in variables to our class's variables. We use the keyword "this" to refer to "this" class's variables. Since the variables being passed in have the same name as the ones for our class, we must use the keyword "this" to refer to the variables for our class vs. the passed in ones.

Line 21: Just to show how a static variable works, everytime any object of this class type is created, we'll add one to the count of total objects of this type. Remember, this is a variable that is assigned to the class itself, not each object that we will create with this class type.

Line 22: We'll just trace out the total number of enemies when each object is created.

Line 27: This function we will call for each enemy whenever we want to damage them, for instance if the player hit them. We will pass in the amount of damage, and have the function decide how to handle it.

Line 29: We'll first check if the total_health is less than or equal to zero, have the enemy tell them it's aleady dead, and exit out of the function (using return; –which means we're returning nothing, which causes us just to exit the function at that point instead of completing it like normal) instead of continuing with the function.

Line 35: Here the total health of the enemy will be taken away by the amount of damage being done.

Line 36–39: We'll check if the health (after the damage has been done), is greater than zero (not dead), and print out the relative message.

Now you have created a class — but what does it do? Nothing yet! We need to create an object of this type of class to be able to do anything with it.

Objects

[...]

No comments:

Post a Comment