Contact Me or check out the Producerism Blog

Tom-as3-gotchi (Flash Game Tutorial) Part 2

05/20/2010


Continuing with our game from part 1, we will now start adding some code to our 2 classes, Tom.as and TomAS3Gotchi.as.  When we left off, the Tom class was basically an empty shell to store various information (health, food, hunger, boredom, etc.) and it had a simple constructor function (when a new Tom class is created, it traces "Hello, I'm Tom!").

Logically, when a new Tom is created, he should have default values set (for example, weight, height, mood and more).  As the project gets bigger and more complex, I may create a class just to store default values, but for now we'll just add them all to the top of Tom.as.  There are 3 main changes:

First, I've created new static constants for all defaults.  Static basically means that the placeholder (DEFAULT_AGE) will always exist within the Tom class.  Constant means that the value of that constant (DEFAULT_AGE=25) will not change while the program is running.  Since DEFAULT_AGE is a constant, trying to write DEFAULT_AGE = 10 anywhere else in the code will cause an error.  The _age placeholder is a variable though (defined with var keyword instead of const), so that means it can change while the program is running.  This is all sort of unimportant now, and more of a personal preference - but I thought it would be useful to explain it in a little bit of detail.

public class Tom
{
// Constants and Defaults
private static const DEFAULT_WEIGHT :uint     = 200;
private static const DEFAULT_AGE    :uint     = 25;
private static const DEFAULT_MOOD   :String = "Happy";
private static const DEFAULT_HEALTH :String = "Healthy";

Next, I've added some more code to the constructor function.  Again, the constructor function is the function with the same name as the class (in this case, "Tom"), which is run automatically when the class is created.  I've added code that sets the _weight, _age, _mood and _health variables to the default constants defined at the top of the class.  I also set _hunger, _thirst and _boredom to 0.  I could have created another set of constants (DEFAULT_HUNGER:uint = 0) but I believe that they will all start at 0 for now, so creating a specific constant just for that seems a bit redundant.

public function Tom()
{
	// constructor function
	trace("Hi, I'm Tom!");

	// set defaults
	_weight = DEFAULT_WEIGHT;
	_age = DEFAULT_AGE;
	_mood = DEFAULT_MOOD;
	_health = DEFAULT_HEALTH;

	// set hunger, thirst and boredom to 0
	_hunger = _thirst = _boredom = 0;
}

Finally, I created 2 getter functions, called health() and mood().  Notice that the health() getter function simply returns the _health variable, and likewise, the mood() function returns _mood.  The reason for creating these functions, are to allow other classes (like TomAS3Gotchi.as) to access information about the Tom.as class.   Without creating public functions like health() and mood(), there would be no way to get the status of Tom's health or mood, since those variables are set to private.

public function get health():String { return _health; }
public function get mood():String     { return _mood; }

The difference between a getter function, and a regular function is pretty simple.  A regular function is written as so:

public function myHealth():String { return "Healthy";  }

and is used like this:

trace(Tom.myHealth());

which will trace out "Healthy" when run.

A getter function is a little different.  To write one, you simple add the word "get" in front of the function:

public function get myHealth():String {return "Healthy";  }

and is used a bit differently too:

trace(Tom.myHealth);

which traces the same message, "Healthy" when run.  The biggest difference is that you can treat a getter function the same as a variable (notice there are no parenthesis after myHealth in this example).  When we get into setter functions later in this series, it will make more and more sense.

The next change is very simple - and that's just to check to see if our changes are working.  In the TomAS3Gotchi.as class, we will add 2 lines to the constructor function, TomAS3Gotchi().

TomAS3Gotchi.as

package
{
	import flash.display.Sprite;

	/**
	 * ...
	 * @author T@IDEAS
	 * Main class of the entire project.  This is the file that is actually compiled to SWF.
	 */
	[SWF(width="800", height="600", backgroundColor="#cccccc", frameRate="30")]
	public class TomAS3Gotchi extends Sprite
	{
		private var _tom:Tom;

		public function TomAS3Gotchi()
		{
			_tom = new Tom();
			trace(_tom.health);
			trace(_tom.mood);
		}
	}
}

Now, when the program is compiled, you get the same trace as before ("Hello, I'm Tom") along with 2 new messages: "Healthy" and "Happy."

Seeing those 2 messages means that not only are the default settings working correctly, but so are our getter functions.  In the next part to this series, I will create similar getter functions for the rest of the variables (hunger, thirst, boredom, etc.) and we will begin to create a very crude GUI for our game, so that there are buttons to click among other things.

All of these files are available to download:

Tom-as3-gotchi Flash Game Tutorial - zip, git, svn, or wonderfl.

Continue to Part 3


Leave a Reply