简体   繁体   中英

Moving a sprite to the left or down is faster than moving it to the right or up

I am making a basic game with Slick2D and LWGJL but I am having a wierd issue that when i'm moving my player(an image) to the left/down it is slower than moving to the right/up.

Input input = gc.getInput();

    if(input.isKeyDown(Input.KEY_W)){
        PlayerY += delta * .1f;
    }
    if(input.isKeyDown(Input.KEY_S)){
        PlayerY -= delta * .1f;
    }

    if(input.isKeyDown(Input.KEY_A)){
        PlayerX -= delta * .1f;
    }
    if(input.isKeyDown(Input.KEY_D)){
        PlayerX += delta * .1f;
    }

All of this code is in the method update()

Edit: All of my code can be viewed in here https://www.dropbox.com/sh/p13sbxucmni36vd/K5XTaNOulm

Any help will be appreciated

  //Setting the original PlayerX and PlayerY values
    private static int PlayerX = Game.ScreenLength/2;
    private static int PlayerY = Game.ScreenHeight/2; 

vs

   if(input.isKeyDown(Input.KEY_W)){
        PlayerY += delta * .1f;
    }
    if(input.isKeyDown(Input.KEY_S)){
        PlayerY -= delta * .1f;
    }

    if(input.isKeyDown(Input.KEY_A)){
        PlayerX -= delta * .1f;
    }
    if(input.isKeyDown(Input.KEY_D)){
        PlayerX += delta * .1f;
    } 

See the problem yet? Change PlayerX and PlayerY(Delta too just in case) to floats and your problem will be solved. Remember when you convert from float to int it always will round down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM