Hey Everyone so been at this for awhile now can't seem to fix this bug I know I am over looking something simple or maybe I am setting it up wrong. I am using Flash Animate and Flash Develop as my IDE.
I am adding my Character player
and the Platforms platforms
that it collides with to the stage Dynamically. Everything seems to be working fine except when I keep jumping the Character falls through the floor a couple pixels at a time and when I jump again it corrects itself but just repeats this scenario over and over. More info The Character is stationary and the platforms are moving to him. All the player has to do is tap to jump.
Here are my variables for Gravity and other objects added to the stage:
//Movie clips
private var player:mcPlayer;
private var platforms:mcPlatforms;
//Jumping Variables
private var nGravity:Number = -1.8;// Gravity
private var nJumpPow:Number = 0; //Y velocity
//Booleans
private var bJumping:Boolean = false;
//Arrays
private var aPlatformArray:Array;
//Add Character
player = new mcPlayer();
player.x = (stage.stageWidth / 2) - 80;
player.y = (stage.stageHeight / 2) + 78;
addChild(player);
private function addPlatForms():void
{
//Add Obstacle Platforms
platforms = new mcPlatforms();
platforms.x = (stage.stageWidth / 2) - 80;
platforms.y = (stage.stageHeight / 2) + 165;
addChild(platforms);
aPlatformArray.push(platforms);
trace(aPlatformArray.length + " NPLATFORMS");
}
Here is my Jump Handler these are in my ENTER FRAME Listener:
private function jumpHandler():void
{
//When User taps screen player jumps
if (bJumping) //Jump Up
{
player.y += nJumpPow;
nJumpPow -= nGravity;
}
}
and Finally here is how I am handling the Collisions:
private function playerPlatformHandler():void
{
for (var i:int = 0; i < aPlatformArray.length; i++)
{
var currentPlatform:mcPlatforms = aPlatformArray[i];
//If user is not touching platform have character fall down
if (!currentPlatform.hitTestPoint(player.x, player.y, true) && !bJumping)
{
trace("PLAYER NOT TOUCHING PLATFORM");
nJumpPow = -15;
player.y -= nJumpPow;
nJumpPow -= nGravity;
}
// Have character stand on top of platform when lands
if (currentPlatform.hitTestPoint(player.x, player.y, true) && bJumping)
{
bJumping = false; //Jumping is false
}
}
trace("JUMPPOWER " + nJumpPow);
trace("GRAVITY " + nGravity);
}
So When I trace the variables I am seeing that the nJumpPow
is increasing and decreasing for each jump which I think might be the culprit but trying to fix that using an if statement and setting its value back to 0 did nothing and the characters feet are still falling through the platform.
Please any help or advice on how to fix this will be much appreciated! Thank you in advance.
nJumpPow
always to -15 before moving player. Ideally you would use different kind of collision detection for a platformer, check some tutorials from google. – kaarto