1
votes

I am making a platform game where the main character moves right and left and jumps however my character jumps and does not return to the ground but stays on top of the stage.My characters movie-clip symbol is called 'naruto' and my ground symbol is called 'ground'.

Here is my code:

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;

naruto.gotoAndStop("stance");
var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var narutoSpeed:Number = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME,gameLoop);


function keyDownHandler(keyEvent:KeyboardEvent):void
{
    if (keyEvent.keyCode == Keyboard.RIGHT)
    {
    rightPressed = true;    
    }
    else if(keyEvent.keyCode == Keyboard.LEFT)
    {
    leftPressed = true;
    }
    else if(keyEvent.keyCode == Keyboard.UP)
    {
    upPressed = true;
    }else if(keyEvent.keyCode == Keyboard.DOWN)
    {
    downPressed = true;
    }

}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
    if (keyEvent.keyCode == Keyboard.RIGHT)
    {
    rightPressed = false;
        naruto.gotoAndStop("standright")
    }
    else if(keyEvent.keyCode == Keyboard.LEFT)
    {
    leftPressed = false;
    naruto.gotoAndStop("standleft") 
    }
    else if(keyEvent.keyCode == Keyboard.UP)
    {
    upPressed = false;
    naruto.gotoAndStop("stance")    
    }else if(keyEvent.keyCode == Keyboard.DOWN)
    {
    downPressed = false;
    naruto.gotoAndStop("stance")    
    }

}

function gameLoop(loopEvent: Event): void {
    //If the right key is pressed, and the left key is NOT pressed
    if (rightPressed && !leftPressed) {
        naruto.x += narutoSpeed;
        naruto.gotoAndStop("right");
    }

    if(leftPressed && !rightPressed) {
        naruto.x -= narutoSpeed;
        naruto.gotoAndStop("left");

    }
var jumpHeight =0;
var defaultJumpSpeed = 20;
var jumpSpeed = 20;




if(upPressed && naruto.hitTestObject(ground))
{
    trace("HELLO!");
naruto.y -= jumpSpeed;
jumpSpeed-= 4;
}


if(upPressed)
{
    trace("HELLO!");
jumpHeight++;
naruto.y -= jumpSpeed;
if(jumpHeight>10)
jumpSpeed -= 4;
}


if(naruto.hitTestObject(ground))
{
    trace("HELLO!");
jumpHeight =0;
jumpSpeed = defaultJumpSpeed;
}
    }

Here is the link for my work: https://www.mediafire.com/?8d5opy49fuqmup5

Here is the problem:

enter image description here

1
Can you be a little more specific on the problem. Does you character stop falling (even though it still looks like it's above the ground)? If so, this likely has to do with the anchor/registration point of your character.BadFeelingAboutThis
My character just floats and stops on the top of the screen.Hamza
So, it goes up and up and doesn't ever come down?BadFeelingAboutThis
yes, sir, i have edited my question can you check my fileHamza
Sorry, I don't download files unless I'm working in isolated VM (which today I'm not). But, you've provided enough code to know what's going on without downloading.BadFeelingAboutThis

1 Answers

0
votes

The main issue, is in your current code, the player can only possible be in 3 positions:

  1. Whatever the ground position is
  2. 16 (up is pressed and the character was not touching the ground
  3. 12 (up is pressed and the character was touching the ground)

Please see the code comments next to your original code to explain what is happening:

//you have this var inside the game loop, so every loop it resets to 20
var jumpSpeed = 20;

//so here, if up is pressed and the character is touching the ground
//you initiate a jump
if(upPressed && naruto.hitTestObject(ground))
{
    trace("HELLO!");
    naruto.y -= jumpSpeed;
    jumpSpeed-= 4;  //since you reset jumpSpeed to 20 every loop, this value will always just be 16
}


if(upPressed)
{
    trace("HELLO!");
    jumpHeight++;

    //if naruto is touching the ground
    //you are subtracting jumpSpeed above and right here again. 
    //so now the y position is 12 if touching the ground (or 16 if not touching the ground)
    //since you reset jumpSpeed to 20 every loop, it always be one of those two values
    naruto.y -= jumpSpeed; 


    if(jumpHeight>10)
    jumpSpeed -= 4;  //this serves no purpose, as your not using the value again and it gets reset to 20 next time the game loop runs
}

//this hit test will succeed in addition to the first one above when your jump starts.
if(naruto.hitTestObject(ground))
{
    trace("HELLO!");
    jumpHeight =0;
    jumpSpeed = defaultJumpSpeed;
}

To remedy your jumping, you'll need to do something along these lines:

//initialize these vars outside of the game-loop for efficient and proper scoping (so their values don't reset every frame)
var isJumping:Boolean = false; //a flag to know if a jump is in progress
var jumpSpeed:Number = 0; //the current velocity of the jump
var defaultJumpSpeed:Number = 20; //the initial force (speed) of a jump
var jumpGravity:Number = 2; //subtract this from the speed every frame to slow the jump over time and fall
var onGround:Boolean = false; //a helper var for efficiency

function gameLoop(loopEvent: Event): void {
    //If the right key is pressed, and the left key is NOT pressed
    if (rightPressed && !leftPressed) {
        naruto.x += narutoSpeed;
        naruto.gotoAndStop("right");
    }

    if (leftPressed && !rightPressed) {
        naruto.x -= narutoSpeed;
        naruto.gotoAndStop("left");

    }

    //only do the hit test once per frame for efficiency (store the result)
    onGround = naruto.hitTestObject(ground);

    if (upPressed && onGround) {
        trace("START JUMP");
        isJumping = true;
        jumpSpeed = defaultJumpSpeed; //set the initial jump velocity
    }

    if(isJumping){ //if jumping
        naruto.y -= jumpSpeed; //move the player based off the current jump velocity
        jumpSpeed -= jumpGravity; //slow the jump every frame (eventually causing it to be negative making the player fall)
    }   

    //touching the ground and the up key is not pressed
    //it's very important to put !upPressed so this doesn't run at the time as the initial jump above
    if (!upPressed && onGround) {
        //stop any jump motion
        jumpSpeed = 0;
        isJumping = false;

        //you probably also want to snap the player to the ground
        naruto.y = ground.y - naruto.height + 2; //the plus 2 ensures that the ground and the player touch so the hit test succeeds
    }
}