0
\$\begingroup\$

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:

My stage

\$\endgroup\$
  • \$\begingroup\$ You only make it accelerate downwards when the up key is pressed \$\endgroup\$ – Bálint Jul 23 '17 at 19:00
  • \$\begingroup\$ Sorry, sir, have tried but it doesn't work.Can you please try to answer the question.Thank you \$\endgroup\$ – Hamza Jul 24 '17 at 10:46
0
\$\begingroup\$

You forgot to add Newton's listens about gravity here :D. change the function gameLoop with mine and add tree variable out of it.

const defaultJumpSpeed = 20;
const gravity:Number = 2 ;
var jumpSpeed = 0;

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");

    }



//New lines by ME
    if(naruto.hitTestObject(ground))
    {
        //The naruto is on the ground
        if(upPressed)
        {
            //The jump button pressing. you may need to prevent these lines to call more than one later
            trace("HELLO!");
            jumpSpeed = defaultJumpSpeed;
            naruto.y -= jumpSpeed ;
        }
        else
        {
            //The naruto is not jumping. stop his falling
            jumpSpeed = 0 ;
        }
    }
    else
    {
        //naruto is falling down, may be he was jumped or he felled to the hole. 
        naruto.y -= jumpSpeed ;
        jumpSpeed -= gravity ;
        jumpSpeed = Math.min(jumpSpeed,-defaultJumpSpeed);// To prevent him from burning like Meteorite because of it's speed
    }
}

After that you need to change the const variable of defauldJumbSpeed and gravity power to change the jump height.

Good luck

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.