A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: smart platforming question

  1. #1

    smart platforming question

    Ive written this for a platformer Im making- its really just for the jumping physics now, and ive been trying to prevent that hopping that happens if you land and are still holding the up button. While i got that to work, this weird little thing happens when you jump, you can mash the up key to slow down your falling speed. Why the heck would that happen?- it should be set up so nothing at all happens when the jump key is hit and the hero isnt both grounded and canJump.

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

    public class Main extends MovieClip
    {
    public var vx:Number;
    public var vy:Number;
    public var gravity:Number;
    public var canJump:Boolean;
    public var grounded:Boolean;

    public function Main()
    {
    vx = 0;
    vy = 0;
    gravity = .7;
    canJump = true;
    grounded = false;

    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    public function onKeyDown(event:KeyboardEvent):void
    {
    if (event.keyCode == Keyboard.LEFT)
    {
    vx = -5;
    }
    else if (event.keyCode == Keyboard.RIGHT)
    {
    vx = 5;
    }
    else if (event.keyCode == Keyboard.UP && canJump && grounded)
    {
    vy -= 10;
    canJump = false;
    grounded = false;
    }
    }
    public function onKeyUp(event:KeyboardEvent):void
    {
    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
    {
    vx = 0;
    }
    if (event.keyCode == Keyboard.UP)
    {
    vy = 0;
    canJump = true;
    }
    }
    public function onEnterFrame(event:Event):void
    {
    vy += gravity;
    hero.x += vx;
    hero.y += vy;

    if (hero.y > 320)
    {
    vy = 0;
    hero.y = 320;
    grounded = true;
    }
    //trace(canJump);
    //trace(grounded);
    }
    }
    }

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    if (event.keyCode == Keyboard.UP)
    {
    vy = 0;
    canJump = true;
    }


    thats the problem
    lather yourself up with soap - soap arcade

  3. #3
    yep that did the trick- thanks!

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