Skip navigation
Currently Being Moderated

Need help with broken code

Dec 13, 2012 2:29 PM

Code: http://pastie.org/5522353

 

So as of right now if I hit my right arrow it will move right with proper acceleration, but  when I go left it moves without acceleration, and space bar doesn't work at all. What is broken?

 
Replies
  • kglad
    72,371 posts
    Jul 21, 2002
    Currently Being Moderated
    Dec 13, 2012 3:31 PM   in reply to Gabriel McKenna

    this looks like a problem:

     

      if(grounded){
        if(hSpeed<=0){
          hSpeed = 0;  // this limits hSpeed to non-neg values when grounded is true
        }else if(hSpeed > 0){
          hSpeed -= 1;
        }else{
          hSpeed ++;  //<-this will never execute
        }

     

    use the trace function to see what booleans are affecting your hero's movement.

     
    |
    Mark as:
  • kglad
    72,371 posts
    Jul 21, 2002
    Currently Being Moderated
    Dec 13, 2012 5:28 PM   in reply to Gabriel McKenna

    that stuttering movement is caused by your code and the way key listeners work.  when you first press the left or right key and hold it down you call accel() but then accel is not called again until the keyboard's delay which varies by system but is about 500ms on my computer.  so, for the first 500ms after pressing one of those keys you get left or right movement and then immediate braking because of your code:

     

    hSpeed, getTimer()

     

    1.5 1573  // <- right key pressed at 1573ms and held down

    1 1589  // immediate slowing

    0.5 1621

    0 1673

    0 1705

    0 1757

    0 1789

    0 1837

    0 1873

    0 1921

    0 1957

    0 2005

    1.5 2037  // keyboard delay expires and now repeated right key events are dispatched.  this is when your hero moves as expected.

    2.5 2089

    3.5 2121

    6 2173

    7 2205

    8 2257

    9 2289

    10 2337

     

    to remedy, use an enterframe loop it increment/decrement hSpeed, not the keyboard event.  use a keydown and keyup listener to determine when to start and stop incrementing/decrementing hSpeed.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points