Question

I'm doing an Orthogonal Tiled Map Java game, when my Ship moves to the x and y bounds pressing a direction Key it stops moving(as intended), but if i keep pushing the Key my character gets out of the Screen.

Here is the code I'm using:

    @Override
    public boolean keyDown(int keycode) {
            ship = world.getShip();

            switch(keycode){
                    case Keys.W:
                        if (ship.getPosition().y<28.5f){

                            ship.getVelocity().y = 1;}
                            else {
                                ship.getVelocity().y = 0;
                            }
                            break;
                    case Keys.S:
                        if (ship.getPosition().y1){
                            ship.getVelocity().y = -1;}
                        else {
                            ship.getVelocity().y = 0;
                    }
                            break;
                    case Keys.A:
                            if (ship.getPosition().x0)
                            ship.getVelocity().x = -1.5f;
                            else{
                                ship.getVelocity().x = 0;
                            }
                            break;
                    case Keys.D:
                            if (ship.getPosition().x<39){

                           ship.getVelocity().x = 1;
                            }
                            else{
                                ship.getVelocity().x = 0;
                            }
                            break;
    default:
                            break;
            }
            return true;
    }

   @Override
   public boolean keyUp(int keycode) {
           ship = world.getShip();
            switch(keycode){
                    case Keys.W:
                            if(ship.getVelocity().y == 1)
                                    ship.getVelocity().y = 0;
                            break;
                    case Keys.S:
                            if(ship.getVelocity().y == -1)
                                    ship.getVelocity().y = 0;
                            break;
                    case Keys.A:
                            if(ship.getVelocity().x == -1.5f)
                                    ship.getVelocity().x = 0;
                            break;
                    case Keys.D:
                            if(ship.getVelocity().x == 1)
                                    ship.getVelocity().x = 0;
                            break;

                    default:
                            break;
            }
            return true;
    }  

What should i do to keep my Ship on Screen if a key is keeping pushed?

Was it helpful?

Solution

What should i do to keep my Ship on Screen if a key is keeping pushed?

Start by using current API's. keyUp() and keyDown() have been deprecated since JDK1.1

Start by reading Motion Using the Keyboard for some more current ideas on how to handle something like this. The code examples do basic bounds checking to make sure you can't move the component off the screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top