I never liked running…

12 10 2010

It’s three in the morning, again, well I can’t really complain about that; these are my working hours after all. But having to debug movement code for the last hour and a half has left me in a sour mood. Of course the fact that I’ve managed to solve it makes me happy. So if nothing else I think we can establish that being a programmer of any kind is one hell of a ride… Bipolar tendencies aside, let’s talk shop.

As mentioned before, I’ve been dabbling into the art of ActionScript 3.0 in order to write a 2D platformer. The language itself has probably been explained by people more familiar with it so I won’t try, I don’t dislike it, in fact its been a pleasent surprise working with it. There are a few niggles here and there, nothing I can’t live with. The one which annoys me the most is the “const” keyword. To me const refers to either bitwise const or logical const, either way when I’m saying: this object is a constant, what I mean is it should not change state [or bits in the memory, however you want to look at it]. There’s something quite wrong when code like this compiles and executes:

const position:Point = new Point(10,10);
position.x=11;
trace(position);

I understand that the const qualifier refers to the actual reference to the underlying object but to me that’s fairly useless. And another thing, would it have killed Adobe to give me control over where/when a const object gets initialized to its final value. Why can’t I use an object’s constructor to determine the value of its constants??

But I started this post by talking about movement code so let’s get back to that. This particular code is part of a class called RunningCore, which is a part of the Player class [and I’m sure you can guess what it does]. I like the concept of having different Core classes which can be quickly added/removed from classes to supply functionality. In contrast, the first iteration of the Player class contained all the running logic inside it and that meant I couldn’t reuse the code for any other object in the game; so I put on my refactoring hat and made some refactoring coffee and got to… well I probably fell asleep, but right after I woke up I went to refactor the code.

Roll your sleeves and let’s go.
What’s the first thing I need from the RunningCore? Why, I need to know whether I’m running right or left; cool, let’s just enum a direction, ah bloody hell, no enumerators in ActionScript… Never mind, constant static integers should work just as well.

public class RunningCore
{
  public static const LEFT  :int = -1;
  public static const RIGHT :int = +1;
  
  public function get IsFacingRight():boolean { return mFacing==RIGHT; }
  public function get IsFacingLeft() :boolean { return mFacing==LEFT;  }

  private var mFacing:int = RIGHT; //arbitrary direction to start at
}

Why, +1, -1? That’s to do with my coordinate system [positive and negative X-Axis] and the fact I already know that the direction would control the Velocity [which is a vector quality] but I don’t want it to resize it. Next I needed to decide how much to fake the physics. In this case I wanted to have an acceleration force which works on the velocity, rather than manipulate the velocity directly. The acceleration itself is constant, unlike real life, but I’m not going for a full physics system here.

Ok, now what?
Well, I know what the overall system should look like, velocity and acceleration. And because I can’t run faster than light players shouldn’t either so let’s cap their velocity/acceleration to some maximum.

public class RunningCore
{
  public static const LEFT  :int = -1;
  public static const RIGHT :int = +1;
  
  public function IsFacingRight() :boolean { return mFacing==RIGHT; }
  public function IsFacingLeft()  :boolean { return mFacing==LEFT;  }
  public function IsRunningLeft() :Boolean { return mCurrentAcceleration<0; }
  public function IsRunningRight():Boolean { return mCurrentAcceleration>0; }
  public function IsMoving()      :Boolean { return mCurrentVelocity != 0; }
  /*Add get/set methods for for velocity and acceleration variables*/

  private var mFacing              :int    = RIGHT;
  private var mCurrentVelocity     :Number = 0;
  private var mMaximumVelocity     :Number = 0;
  private var mCurrentAcceleration :Number = 0;
  private var mMaximumAcceleration :Number = 0;
}

I’ve omitted the get/set methods for brevity’s [and formating] sake. Notice how I’ve also made some query functions [which could also be get functions I guess, if you’re into that sort of thing] which cement the requirements I have from this class.

Are you moving? 
Where are you moving? 
How fast are you moving?

This post is getting long and I still need to reimplement the jump mechanics, so I’ll just break it off now. The next post will be tomorrow [rather than a week from now] and will finish the RunningCore class.

“The chances of anything coming from Mars are a million to one he said”

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: