I’m trying to make a platformer and I’m having a problem where the character will clip half through the ground upon landing from a jump
Just so you have a clear picture – right now all that’s on the stage is the character (which the code is placed in) and the ground (a completely flat plane called ‘ground’)
Here’s the code:
onClipEvent(load)
{
jumpHeight =0;
defaultJumpSpeed = 20;
jumpSpeed = 20;
}
onClipEvent(enterFrame)
{
if((!Key.isDown(Key.SPACE)) && (!this.hitTest(_root.ground))) //when we stop jumping
{
this._y -= jumpSpeed; //we continue moving up to help create a curve
jumpSpeed-= 4; //constatly decrease jump to create curved jump
}
if((Key.isDown(Key.SPACE)) ) //if we're jumping
{
jumpHeight++;
this._y -= jumpSpeed;
if(jumpHeight>10)
jumpSpeed -= 4; //constantly decrease to form curve
}//end of jumping
if(this.hitTest(_root.ground))
{
jumpHeight =0; //reset jumpHeight
jumpSpeed = defaultJumpSpeed; //reset jumpSpeed
}
if(Key.isDown(Key.LEFT)){
this._x -= 20;
}
if(Key.isDown(Key.RIGHT)){
this._x += 20;
}
}//end
(btw, I know I don’t have a max fallspeed for the character in this code)
I’m pretty sure I know what the problem is. Right now the character’s jump constantly decreases to give the effect of gravity. It starts out positive (character moves up), then goes negative (character falls down). The character will only stop falling if they come in contact with the ground (in the form of a hit test). The problem (I think) is that the character can fall at, say, 36 pixels a second or more, so sometimes they’ll be half through the ground before the hit test goes off and stops him from falling further.
Do think this is correct or am I missing something. If this is right, how do I stop it without making the character fall-rate really, really low
thanks in advance