Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>On Update, you can simply check the array. If it is a wall don't update. If it is the border, don't update.</p> <pre><code>@Override public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { Input input = gc.getInput(); float yChange = 0, xChange=0; boolean capturedInput = false; if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) { player = movingUp; yChange += delta * SPEED; capturedInput = true; } else if (input.isKeyDown(Input.KEY_DOWN) || input.isKeyDown(Input.KEY_S)) { player = movingDown; yChange -= delta * SPEED; capturedInput = true; } else if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_A)) { player = movingLeft; xChange += delta * SPEED; capturedInput = true; } else if (input.isKeyDown(Input.KEY_RIGHT) || input.isKeyDown(Input.KEY_D)) { player = movingRight; xChange -= delta * SPEED; capturedInput = true; } if(capturedInput==true &amp;&amp; !blocked(playerX+xChange,playerY+yChange)){ playerX += xChange; playerY += yChange; player.update(delta); } } private boolean blocked(float x, float y) { return blocked[(int) x][(int) y]; } </code></pre> <p>Pretty much, do not change the value until you check it first and then change the player value if all is okay.</p> <p>EDIT: I added a variable capturedInput, this will let me know that we actually have a keyDown event. There are two comments about this. I consider this a "hack" to make your code work and not the most beautiful. I would personally try to add an event listener to the window to listen for key clicks, when a key is clicked I would change the position information, all the while the frame is refreshing and will update, or only refresh now that there was a change by calling repaint().</p> <p>There are lots of tutorials out there for listening to onKeyDown. Your code should work better now and won't need the manual catch for the edge like your doing, you should be able to draw a wall around the edge in the block array of booleans, if not you can continue with your method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload