A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Need Help troubleshooting a ball jumping

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2

    Need Help troubleshooting a ball jumping

    My ball jumps fine and lands fine but if im jumping to the side and i let go of the space he just stops going left or right and drops to the ground i need him to be able to keep going in the direction i pressed even if i release the jump button this is the code i have

    package
    {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class PlayerJumping extends Sprite
    {
    private var ball:Ball;
    private var vx:Number = 0;
    private var vy:Number = 0;
    private var ax:Number = 3;
    private var ay:Number = 20;
    private var gravity: Number = 2;
    private var floor:Number = 300;
    private var jumping: Boolean;

    public function PlayerJumping()
    {
    init();
    }
    private function init():void
    {
    ball = new Ball ;
    addChild(ball);
    ball.x = stage.stageWidth / 2;
    ball.y = stage.stageHeight / 2;

    addEventListener(Event.ENTER_FRAME,onEnterFrame);
    stage.addEventListener(KeyboardEvent.KEY_DOWN,onKe yDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    }
    private function onEnterFrame(event:Event):void
    {
    vy += gravity;
    ball.x += vx;
    ball.y += vy;
    onFloor();
    }
    private function onKeyDown(event:KeyboardEvent):void
    {
    switch (event.keyCode)
    {
    case Keyboard.LEFT :
    vx = - ax;
    break;

    case Keyboard.RIGHT :
    vx = + ax;
    break;

    case Keyboard.SPACE :
    if(jumping == false)
    {
    vy = - ay;
    jumping = true;
    }
    break;

    default :
    break;
    }
    }
    private function onKeyUp(event: KeyboardEvent):void
    {
    vx = 0;
    }

    private function onFloor(): void
    {
    if (ball.y > floor)
    {
    ball.y = floor;
    jumping = false;
    }
    }
    }
    }

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    delete this

    Code:
    private function onKeyUp(event: KeyboardEvent):void
    {
    vx = 0;
    }
    lather yourself up with soap - soap arcade

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Yeah when i delete that then when i press left and right it wont stop moving if i let it go

  4. #4
    Senior Member Draxus's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    123
    Get rid of the keyup and add this to your enterframe

    vx *= .97 (or whatever number feels good)
    vy *= .97

  5. #5
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Just check if you're jumping in the KeyUp function. If you're jumping, don't set vx to 0. Then in the onFloor function set vx to 0, and afterwards it should all go good again.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center