The script:
//Let's get started!
onClipEvent(load){
The script between this and its terminal curly bracket will execute once when the clip is first loaded
stop();
var pa_dx:Number = 0;
var pa_dy:Number = 0;
var pa_dir:Number = 0;
var pa_runspeed:Number = 6;
var pa_jumpheight:Number = 16;
var pa_gravity:Number = 1.2;
var pa_fallspeed:Number = 0;
var pa_jumped:Boolean = false;
var pa_falling:Boolean = true;
var pa_death:Boolean = false;
var pa_fallcheck:Boolean = false;
var pa_damage:Boolean = false;
var pa_invuncount:Number = 0;
// This is where all variables are defined. Defining their types strictly is a good practice to get into.
function pa_landing():Void{
pa_fallcheck = true;
pa_falling = false;
pa_fallspeed = 0;
pa_jumped = false;
pa_airspeed = 0;
pa_airset = false;
if(pa_damage){
pa_invuncount = 80;
pa_damage = false;
}
}
/*A function to be called when the character lands on a surface. This block of code will be called more than once, so it can shorten your code dramatically if you put it in a function.
It resets variables to their values while on ground, as well as initiating the second stage of my damage process where the character experienes a brief period of invilnerability if he has been damaged.*/
function damaged(dmg:Number):Void{
pa_damage = true;
pa_jumped = false;
pa_fallspeed = -10;
}
//A function which can be called if the character is hit by an enemy. It features an input number if you wish to have the health drained more by certain enemies or attacks. If you wish to use this input, add a line saying _root.healthvariable -= dmg and add the input when you call the function (_root.char.damaged(#Number)). You will need to define a _root variable for health for this to work.
}
onClipEvent(enterFrame){
//The following will execute on every frame of your game.
//Movement and gravity
if(!pa_death){
//If your guy is not dying
pa_dir = (Key.isDown(39) - Key.isDown(37));
//Here's a nice bit of binary. If either key is down the Key.isDown(#); is true, and returns this. The true can also be read as a one if used as a number, while false is a zero. So, if neither or both keys are down, the direction is 1 - 1 or 0 - 0 which is zero. If left or right are down the direction becomes -1 or 1 respectively.
_x += pa_runspeed * pa_dir * !pa_damage;
/* Increases the character's _x position by his speed multiplied by direction (-1, 1 or 0, remember?)
We have more binary here - instead of using an if(); statement the value is multiplied by the variable. If the damage variable is false, !pa_damage returns true (! = not), with a value of one, and Number * 1 = Number. If, however, it is true, !pa_damage returns false, so Number * 0 = 0, and the speed becomes 0. Result- you guy will not move. */
if(!pa_falling){
//Remember - !variable will return true if the variable is false and vice versa. This statement means thecode will execute if the falling variable is false.
pa_jumped = Key.isDown(38);
//Sets the value of jumped to the value of the jump key being down - true if it is down and false if it is not.
}
pa_fallspeed += pa_gravity * (pa_fallspeed - (pa_jumpheight * pa_jumped) < 20);
/*This is all to do with falling, so pay attention. Unlike many engines for platformers, with mine you are always falling and being pushed back up, a bit like real life physics.
Now, this bit of code increases the speed of falling by the gravity variable multiplied by whether the speed is below a certain point. As a result, if the speed exceeds a certain point then the part in brackets returns a value of false, or 0, and gravity * the comparison becomes 0, so the speed of falling no longer increased. Stuff like this can seem complicated at first, but the more you can get into it the simpler it becomes.
_y += pa_fallspeed - (pa_jumpheight * pa_jumped);
//This again utilises binary. It increases the character's _y position by the speed of falling (in Flash the _y axis is inverted), but the speed of falling minus the jumpheight multiplied by the value of jumping (true or false; 1 or 0). So, if the character is jumping, and since the speed of falling is reset to 0 on landing, immediately after you jump the fallspeed variable is a negative and so the character goes up instead of down. At least, untill gravity catches up.
_y += 5 * pa_fallcheck;
pa_fallcheck = false;
/*This is a line I always make a point of adding in these engines. The fallcheck variable is a failsafe which means that on the first frame away from the ground your _y position is reduced by 5 pixels.
This does make things closer to real life - when you're walking downhill or down stairs you don't just walk straight forward and let gravity do all the work. You step downwards and continue walking. The same is true in games.
If this value wasn't here then the character would go into falling mode over ridicolously short slopes, which would look particularly bad if you had his animation tied into whether he was falling or not. A side effect of this is that in the first frame of jumping he can rise slower than in the second, but this really isn't noticable particularly in higher framerates, and isn't really worth wotrking around.
if(pa_invuncount > 0){
_visible = ((pa_invuncount % 6) >= 3);
pa_invuncount --;
}
/*This is the second stage of what happens when you recieve damage. In the first you jump into the air, and upon landing move to the second. Here you experience a brief, in this case 80 frame burst of invulnerability avter you are damaged. There are games where you recieve damage as long as you are in contact with an enemy, and this is a way around this.
It also removes any unfairness involving repeatedly taking damage from the same enemy whenever you hit the ground. Try to include something like this in your game.
This code makes use of a flicker effect. For further information on this see my other tutorial in AS:Main.
else{
_visible = true;
}