Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 904815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:06:48+00:00 2026-05-15T16:06:48+00:00

OK so I have this applet which lets a player move his 32×32 character

  • 0

OK so I have this applet which lets a player move his 32×32 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. 😀 I’m sure the problem lies within the map board tile… :\ My guess…

Thanks,
Dan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:06:49+00:00Added an answer on May 15, 2026 at 4:06 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Java Applet built using AWT. This applet lets you select pictures
I have applet which I run in command line like this: C:\Program Files (x86)\Java\jdk1.6.0_21\bin\appletviewer.exe
I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
I have an applet. In this I have a JLabel component. When the user
I have an applet that throws this exception when trying to communicate with the
I have an Applet which is loading images over a http connection using URLConnection.
Lets say i have an application which has three text-fields, and i can type
I have an applet which contains some images, regular java classes and class, which
i have written an Java Applet, which is loaded in HTML and its function
I have applet which use jna Pointer class. The applet code is: import com.sun.jna.*;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.