Skip to main content
Known Participant
January 4, 2013
Question

How to Make Character Jump OnPress of Button

  • January 4, 2013
  • 1 reply
  • 1144 views

Hi guys please help me on this one. I am so noob in this actionscript programming

I am making a runner, side scrolling game for a android phone. It is like World of Rabbit(WOR) or iRunner. It moves automatically to the right, so the problem is

I want my character to jump when I press the jump button then after that when it lands it will continue running.

Please help guys thanks in advance.

This topic has been closed for replies.

1 reply

Inspiring
January 4, 2013

An example would be...

var run:Boolean = true;

var runSpeed:Number = 5;

var jumping:Boolean = false;

var jumpSpeedLimit:int = 10;

var jumpSpeed:Number = 0;

jump_btn.addEventListener(MouseEvent.CLICK, jump);

function jump(event:MouseEvent):void

{

    mainJump();

}

character.addEventListener(Event.ENTER_FRAME, moveCharacter);

function moveCharacter(event:Event):void{

   

    if(run){

        character.x += runSpeed;

    }

    if(jumping){

        mainJump();

    }

}

function mainJump():void{

   

    if(!jumping){

       

        jumping = true;

        jumpSpeed = jumpSpeedLimit*-1;

        character.y += jumpSpeed;

    } else {

       

        if(jumpSpeed < 0){

            jumpSpeed *= 1 - jumpSpeedLimit/75;

            if(jumpSpeed > -jumpSpeedLimit/5){

                jumpSpeed *= -1;

            }

        }

        if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){

            jumpSpeed *= 1 + jumpSpeedLimit/50;

        }

        character.y += jumpSpeed;

       

        if(character.y >= stage.stageHeight - character.height){

            jumping = false;

            character.y = stage.stageHeight - character.height;

        }

    }

}

Known Participant
January 5, 2013

Thanks a lot mate

The codes work but it is kinda not what i am looking for dude. I am doing a runner game so the background is moving to the left, then my character has a animation that is running to the right but not moving at it's position at all.

What i want is, the character to jump then land at the same position, it does look like it's moving because the background is moving.

Sorry about my first post, I think my question is kinda confusing.

Mark.fromOP
Inspiring
January 7, 2013

Well you need a jump listener, so when the user hits a button on the screen or jerks the phone (if you want to tap into the accelerometer) you then make the character respond, you probably have a run animation and you probably have a jump animation, they should be all in the same movie clip. When the user hits a button for the jump have the character movie clip skip to the frames that show the jump animation myCharacter.gotoAndPlay("jumpAnimation"), you will have to have a frame label set up called "jumpAnimation" or you can just tell the movie clip to go to a frame number myCharacter.gotoAndPlay(5); at the same time you can also move the whole movie clip up if the animation does not already do this, I suggest using a tween, my favorite is tweenLite.

Hope that makes some sense. Seems like a complex first project since you will also have to listen for collisions.