0
votes

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.

1
When player hits floor, you'll have to check if player is below floor surface or not, and move it a bit up when it's too low. As it now moves 15 pixels per frame, it might move up to 15 pixels too low. Also, now your gravity has no effect since you reset 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

1 Answers

0
votes

I have some hints for you. I hope one of them could solve your main problem.

1- You have to fall your player when you are sure it is not hitting any of your floors. but in your codes, I can see that you are controlling all the floors and you are changing the player y when you are catching that the player is not hitting any of them. in many cases, player could stand on a floor but there are many floors left that player couldn't touch them.

You have to correct your function like below:

 private function playerPlatformHandler():void 
{
    var isPlayerOnAFloor:Boolean = false ;
    for (var i:int = 0; i < aPlatformArray.length; i++)
    {
        var currentPlatform:mcPlatforms = aPlatformArray[i];

        // Have character stand on top of platform when lands
        if (currentPlatform.hitTestPoint(player.x, player.y, true) && bJumping)
        {
            isPlayerOnAFloor = true ;
        } 
    }

    if(!isPlayerOnAFloor)
    {
        //Fall him down now!
    }
}

2- To have a better performance, Allays bring flag variables to the left side of complicated calculation in the if(flag & calculation) condition place. the the code will not continue for calculations if the flag was false. the hitTest is not a simple calculation specially when you have big floors!

Let me know if your problem solved with 1'th solution.