3 Replies Latest reply: Dec 13, 2012 5:28 PM by kglad RSS

    Need help with broken code

    gabessdsp Community Member

      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?

        • 1. Re: Need help with broken code
          kglad MVP

          this looks like a problem:


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


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

          • 2. Re: Need help with broken code
            gabessdsp Community Member

            I decided just to recode the whole thing and now I have it all working. Here is my new code: http://pastie.org/5522782


            And here is what it turns out to be:




            when you click on the left, right, a, or d key you move across the x-axis, but you notice there's a small jump, then my hero shakes a little bit, then moves smoothly, how do I go about fixing this?

            • 3. Re: Need help with broken code
              kglad MVP

              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.