views:

27

answers:

1

OK so I have this applet which lets a player move his 32x32 character between tiles ... and anytime he is on the edge of the map then MOVES anywhere else... I get an ArrayIndexOutOfBounds exception. Also, when this happens, the character can walk through blocked tiles! However, this only happens on the east and south edges but on the south edge, the character CANNOT walk through blocked tiles upon moving away from the edge.

I don't know how to fix this and maybe you could help me?

Here is an image of me explaining my problem:

applet

Here is the code:

/** Tile Generator Programmer: Dan J. Thanks to: g00se, pbl, Manny. Started May 23, 2010 **/

import java.awt.; import java.awt.event.; import java.applet.Applet; import java.io.; import java.util.;

public class tileGen extends Applet implements KeyListener {

Image[] tiles; // tile arrays Image player; // player image int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile x - tile y boolean left, right, down, up, canMove, respawn; // is true? int[][] board; // row tiles for ultimate mapping experience! final int NUM_TILES = 33; // how many tiles are we implementing? Label lx, ly; // to see where we are! int r1, r2, u1,u2,l1,l2,d1,d2;

int lastX, lastY, row, col; Label lbl1, lbl2, p1, p2;

public void init() {

board = loadBoard();

tiles = new Image[NUM_TILES];     for(int i = 0;i < NUM_TILES;i++) {
  tiles[i] = getImage(getClass().getResource(String.format("tiles/t%d.png",

i))); }

    player = getImage(getClass().getResource("player.png"));

// our player addKeyListener(this); canMove = true; int spawnX = 4; int spawnY = 4; px =0; py =0; lastX = 0; lastY= 0; lbl1 = new Label("LastX", Label.LEFT); lbl2 = new Label("LastY", Label.LEFT);

      p1 = new Label("X", Label.LEFT);
      p2 = new Label("Y", Label.LEFT);
       add(lbl1);
       add(lbl2);

       add(p1);
       add(p2);
       this.setFocusable( true );
}

private static final HashSet<Integer> BLOCKED_TILES = new

HashSet(); static {
BLOCKED_TILES.add(24);
BLOCKED_TILES.add(0);
BLOCKED_TILES.add(6);
BLOCKED_TILES.add(25);
BLOCKED_TILES.add(3); //add more tiles here }

public void keyPressed(KeyEvent e) {

if (isInBound(lastX,lastY) == true) { System.out.println("\nYOU WENT OFF THE GRID.\n"); }

  if (lastX > 0) {        r1 = lastX + 1;
  }else{      r1 = 0;         }       r2 = lastY;

  u1 = lastX;         if (lastY > 0) {         u2 = lastY - 1;         }else{
   u2 = 0;
   }



  if (lastX > 0) {        l1 = lastX - 1;
  }else{      l1 = 0;         }       l2 = lastY;

  d1 = lastX;         if (lastY > 0) {            d2

= lastY + 1; }else{ d2 = 0; }

  right = true;       left = true;        up =

true; down = true;

try { if (blocked(r1,r2) == true) right = false; // we cannot go right if (blocked(u1,u2) == true) up = false; // we cannot go up if (blocked(l1,l2) == true) left = false; // we cannot go left if (blocked(d1,d2) == true) down = false; // we cannot go down

  }catch(ArrayIndexOutOfBoundsException

dap) { System.out.println("Array Index is Out of Bounds...:(\n" + dap.getCause()); }

if (left == true) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { left = true; px = px - 32; lastX = lastX - 1; } }

if (right == true) { if (e.getKeyCode() == KeyEvent.VK_RIGHT) { right = true; px = px + 32; lastX = lastX + 1; } }

if (down == true) { if (e.getKeyCode() == KeyEvent.VK_DOWN) { down = true; py = py + 32; lastY = lastY + 1; } }

if (up == true) {

      if (e.getKeyCode() ==

KeyEvent.VK_UP) { up = true; py = py - 32; lastY = lastY - 1; } }

String txtLastX = Integer.toString(px); lbl1.setText(txtLastX);

String txtLastY = Integer.toString(py); lbl2.setText(txtLastY);

String txtLastX1 = Integer.toString(lastX); p1.setText(txtLastX1);

String txtLastX2 = Integer.toString(lastY); p2.setText(txtLastX2); repaint();

} public void keyReleased(KeyEvent e){

} // ignore public void keyTyped(KeyEvent e){} // ignore

public void paint(Graphics g) {

    for (row = 0; row < board.length; row++) {
        for (col = 0; col < board[row].length; col++) {
            int index = board[row][col];
            g.drawImage(tiles[index], 32 * col, 32

* row, this);

        }
    }
    if (respawn == false) {
    g.drawImage(player, px, py, this);    }   if (respawn == true) {      

g.drawImage(player, 0,0, this);
System.out.println("Respawned!");
respawn = false; } } // end paint method

 public void update(Graphics g)
 {
      paint(g);
 }

public int[][] loadBoard() {
    int[][] board = {
            { 2,2,24,24,24,24,24,1,3,0,0,0 },
            { 2,2,24,23,23,23,24,1,3,0,0,0 },
            { 1,1,24,23,23,23,24,1,3,3,3,1 },
            { 1,1,24,24,23,24,24,1,1,1,1,1 },
            { 1,1,1,1,7,1,1,1,1,1,1,1 },
            { 5,1,1,1,7,7,7,7,7,1,1,1 },
            { 6,1,3,1,1,1,3,1,7,7,7,1 },
            { 6,1,3,1,3,1,1,1,1,1,7,3 }
        };    return board;
}

public boolean blocked(int tx, int ty) { return BLOCKED_TILES.contains(board[ty][tx]); }

public boolean isInBound(int r, int c) { return (r >= 0) && (r < 8) && (c >= 12) && (c < 1); }

} // end whole thing

If this was solved, this would make me extremely not sad. :-D I'm sure the problem lies within the map board tile... :\ My guess...

Thanks, Dan

+1  A: 

I quote:

if (lastX > 0) {        r1 = lastX + 1;
  }else{      r1 = 0;         }       r2 = lastY;

  u1 = lastX;         if (lastY > 0) {         u2 = lastY - 1;         }else{
   u2 = 0;
   }

You only increment X if it's already greater than 0? I think you want to make sure it's less than the biggest allowed value for x!

EDIT: In more detail:

Here's your check for going right:

if (lastX > 0) {        r1 = lastX + 1;
  }else{      r1 = 0;         }       r2 = lastY;

  u1 = lastX;         if (lastY > 0) {         u2 = lastY - 1;         }else{
   u2 = 0;
   }

...and here's your check for going left:

  if (lastX > 0) {        l1 = lastX - 1;
  }else{      l1 = 0;         }       l2 = lastY;

You're checking the same thing in both cases! That's got to be logically wrong. The first check is, in fact, the reason you get the exception going right: You ask if it's safe to go left, then you go right!

Carl Smotricz
I'm sorry that made no sense to me. Sorry, English isn't my first language... :(
Dan
But what do you suggest I do?
Dan
@Dan - try again. It makes sense to me. *"But what do you suggest I do?"*. His second sentence explains that.
Stephen C
See my edit. If that doesn't help, just learn to work more carefully.
Carl Smotricz
@Carl - *"... learn to work more carefully"* ... Agreed. My impression is that the OP's code is poorly thought out and poorly implemented; e.g the formatting is horrible.
Stephen C
Actually, it's pretty nicely indented. The formatting on StackOverflow messes it up. The code is thought up just fine, I just think I make it harder than it has to be... :\
Dan
@Dan - don't blame StackOverflow. The formatting problem is partly the crappy way that you dumped the source code into the edit window, and partly the fact that your source code contains hard TAB characters. The latter is a huge stylistic sin, in my opinion. As far as I'm concerned it is too much to expect SO folks to help you with code that they cannot read. It is **your** responsibility to get the formatting right.
Stephen C
Um, in the previous codes my indenting was fine and all but when I pasted to Stackoverflow.. it was bad. Maybe I'm just suck at following StackOverflow's style...
Dan
In Dan's defense, there is still a huge and fierce debate over The One True Style among programming professionals, with some of them still making valid arguments for tabs. Eclipse and other IDEs still come with tabbing in their default settings, which I hurriedly change. But in this particular case it's true that, when you're thinking of pasting your code into SO, tabs are a big pain.
Carl Smotricz