Left, right, left, right

13 10 2010

Last time we’ve seen our little player character he, well, he couldn’t really do anything because I didn’t finish the class. Although you could ask him some serious questions about his running. Let’s fix that.

What do I need next?
A question which drives all of creation. In this case what is needed is a way to communicate between the user of the RunningCore and the RunningCore itself. Since I’ve already established that users issue a command to accelerate [ie. they tell the RunningCore to apply acceleration], this would be the name of my new function. But accelerating alone doesn’t help, I also need to know which direction to accelerate to.

public function Accelerate(_direction:int):void

The first thing I need to make sure is that users [me later on when I forget how to use my own code] don’t break the logic behind this function and supply a “funny” value.

public function Accelerate(_direction:int):void
{
  //assert(_direction==LEFT || _direction==RIGHT);
  mFacing=_direction;
}

The assert is not a macro in ActionScript as far as I know, just replace it with the appropriate fault-handling routines of the project. Alright, the player can change direction but still no acceleration, let’s fix that too.

public function Accelerate(_direction:int):void
{
  //assert(_direction==LEFT || _direction==RIGHT);
  mFacing=_direction;
  mCurrentAcceleration = mMaximumAcceleration * mDirection;
}

Notice how it’s not really proper physics… Well, this is a small 2D platformer in ActionScript. Control and fun take precendence over physical accuracy [usually].

Alright, I’ve got a way to communicate to the RunningCore that I want to accelerate. Now what?
the next thing is a way to update the RunningCore’s velocity. A good function for that, lo and behold;

public function Update(_deltaTime:number):void
{
}

Taking the simplest route possible following our “fake” physics;

public function Update(_deltaTime:number):void
{
  mCurrentVelocity += mCurrentAcceleration * _deltaTime;
}

Of course using this players can’t actually stop and in fact will continue to accelerate until they hit lightspeed [and if you want to know what that feels like, watch Red Dwarf]. First order of business, let’s cap velocity to its maximum. For that, two more functions, this time private since they’re used for internal logic.

private function IsVelocityAtMaximum():boolean
{
  return (Math.abs(mCurrentVelocity) >= mMaximumVelocity);
}
private function CapVelocityToMaximum():void
{
  mCurrentVelocity = mMaximumVelocity * Tools.GetSign(mCurrentVelocity);
}

Why all the exta functions?
A. I like them and it’s my blog 😛
B. And probably more important, because this is an evolving code. And separate functions are easier to debug/trace/change as needed. Also, it’s a lot easier to copy/paste code away from functions then try to refactor loose code into functions!

But wait, why is it taking the absolute value of mCurrentVelocity? and what’s this Tools.GetSign()??

The IsVelocityAtMaximum() function needs to take the absolute value of mCurrentVelocity since the value of velocity may be positive [if running right] or negative [if running left] but the mMaximumVelocity is a positive value [since I’ve decided to use the same value for both positive and negative velocities].

As for the second question, the Tools.GetSign() is a function which returns either +1 or -1 depending on the value of its argument. I wasn’t sure whether ActionScript had this built in.

public function GetSign(_value:number):int
{
  return (_value>=0) ? +1 : -1;
}

I love this operator 😀 And mCurrentVelocity needs to be capped in relation to its sign [direction]. Meaning that if the user is moving left [-1] mCurrentVelocity needs to be capped to -mMaximumVelocity and vice-versa.

Now for another decision, isn’t programming fun! Do I want to disable acceleration if the current velocity is passed the maximum, or do I want to cap it to the maximum when needed? Let’s disable it for now though.

public function Update(_deltaTime:number):void
{
  if (!IsVelocityAtMaximum())
  {
    mCurrentVelocity += mCurrentAcceleration * _deltaTime;
  }
}

Well, 3am is my cue to accelerate into bed [going to leave the dishes dirty tonight :P]. Tomorrow will see the conclusion of this little running saga [horrible horrible pun, please don’t shoot me].

“Lord knows I’ve paid some dues gettin’ through; Tangled up in blue.”

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: