Differences in responding to Left and Right key presses.

Started by mrd, December 15, 2013, 13:09:41

Previous topic - Next topic

mrd

Hi,

first post and an odd problem here but please excuse any naivety.  I'm programming a simple (wish) platform game but am having some fun with the left and right key presses.   I'm using the Sonic Physics guide for help.  http://info.sonicretro.org/SPG:Running

Left works fine but right doesn't - it's too slow to respond in a key frame.  One press of right results in an initial value of

Right:0.046875
Right:0.09375
Right:0.140625
Right:0.1875
Slowing:0.1171875
Slowing:0.046875
Slowing:0.0


but the same on left gives

Left:-0.046875
Slowing:0.0
Left:-0.046875
Slowing:0.0
Left:-0.046875
Slowing:0.0
Left:-0.046875
Slowing:0.0


This method responds to the key presses and is called every frame.
   private void frameUpdate() 
    {
        //Update the sensor lines so we know when to stop the player
        player.updateSensorLines();
        
        //Basic keyboard movements
        if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
            if (isBlockedLeft()) { player.dx = 0; }
            else { 
                player.updateSpeed(-1); 
            }
        
        } if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
            if(isBlockedRight()) { player.dx = 0; }
            else {
                player.updateSpeed(1); 
            }
        
        } else if(player.dx != 0) {
            player.updateSpeed(0);
        }
        
        if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
            System.out.println("Jump/Space Pressed");    
        }
        player.update();
       
    }


This code updates the speed
    public void updateSpeed(int dir) 
    {
        switch(dir) {
            case -1: //Left
                dx += -ACCELERATION;
                if (dx < -MAX_SPEED) {
                    dx = -MAX_SPEED;
                }
                System.out.println("Left:"+dx);
                break;
            case 1: //Right
                dx += ACCELERATION;
                if (dx > MAX_SPEED) {
                    dx = MAX_SPEED;
                }
                System.out.println("Right:"+dx);
                break;
            default: //No Key Pressed so let friction occur
                if (dx < -FRICTION) { dx += FRICTION; }
                if (dx > FRICTION) { dx += -FRICTION; }
                else { dx = 0; }
                System.out.println("Slowing:"+dx);
                break;
        }      
    }


I'd really welcome some ideas here because it doesn't make sense to me that they are behaving differently.

Thanks

Chris

mrd

Ignore me.   I sussed it.  I was missing an else .
I wouldn't normally continue an if on the same line as a close without an if so didn't see it. 
What a waste of time this morning !!!

Thanks for viewing!