AS2
Well, due to so many platformers having the old issue of the character sticking into the ground or so, i decided something has to be done, and so i made this tutorial. Things you need to know: Loops-for... in and have a basic mathematical ability.
1) Gravity
many platoformers use a code like this:
grav ++
char._y += grav
Although this works, it is unlike real gravity's acceleration (9m/s^2)
Its not that complicated, simply put, you need a second gravity variable.
grav ++
yvel += grav
char._y += yvel
HitTesting
This is the bit that made me want to start this tutorial. Hittesting at the moment is not up to scratch in my opinion. At the moment, most platformers use something along these lines:
if(ground.hitTest(char._x, char._y, true)){
grav = 0
}
Works ok, until the character gets stuck in the ground. Now you could make a while loop raise him up, but a character can still completely fall through the ground. I bet you dont know why it does this though do you? here is the reason: Imagine the gravity variable is at 20. This means the character's _y position will be move down 20 pixels. Now, imagine the ground is only 10 pixels below the character, that means the character will en up 10 pixels below the ground and if that ground is only 5 pixels high, it means that the character wont even see the ground there. Now, using for loops we can check every single pixel below the character, but not so far that it extends the gravity variable. Generally not even 100 loops of a basic hittest like this will generate any lagg, and if gravity is reaching that high, you've got terminal velocity issues :].
so a basic code for something like this would look like this.
for (var i:Number = 0; i<yvel; i++) {
//for loop
if (ground.hitTest(char._x, char._y+i, true)) {
//hitTest. Remember, you have to incoparate i into the _y cheak point
char._y += i;
//Remember to then change the char._y to what i is at that moment
yvel = 0;
grav = 0;
//dont forget to reset your gravity variable, or it wont make any difference
break;
//and add a break; just so when the hittest is actually true that the loop stops.
}
}
also remember, try to still keep the ground thicker then 10 if you plan on having alot of other code, just for incase.
Example .fla
Example .swf
you can jump using up arrow
Any questions, please feel free to ask