好的,我有这个小程序可以让玩家在瓦片之间移动他的32x32字符......并且任何时候他在地图的边缘,然后在其他地方移动...我得到一个ArrayIndexOutOfBounds异常。而且,当发生这种情况时,角色可以穿过阻塞的瓦片!然而,这只发生在东部和南部的边缘,但在南部的边缘,角色不能在从边缘移开时穿过阻塞的瓷砖。Java小程序 - ArrayIndexOutOfBoundsException
我不知道如何解决这个问题,也许你可以帮助我?
这里是我的解释我的问题的图像:
applet http://i26.tinypic.com/64qujt.jpg
下面是代码:
/**瓦发电机程序员:丹J. 感谢:g00se, pbl,曼尼。开始 2010年5月23日 **/
import java.awt。 ; import java.awt.event。; import java.applet.Applet; import java.io. ; import java.util。;
公共类tileGen延伸的Applet 实现的KeyListener {
图像[]瓦片; //瓷砖阵列图片 玩家; //播放器图像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; // 是真的? int [] [] board; //排列最终的 贴图体验! final int NUM_TILES = 33; //我们实施了多少个瓷砖 ?标签lx,ly; //到 看看我们在哪! int r1,r2, u1,u2,l1,l2,d1,d2;
int lastX,lastY,row,col;标签 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"));
//我们的球员 addKeyListener(本); canMove = true; int spawnX = 4; int spawnY = 4; px = 0; py = 0; lastX = 0; lastY = 0; lbl1 =新标签(“LastX”,Label.LEFT); lbl2 =新标签(“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();静态{
BLOCKED_TILES.add(24);
BLOCKED_TILES。(0)加;
BLOCKED_TILES.add(6);
BLOCKED_TILES.add(25);
BLOCKED_TILES.add(3); //在这里添加更多 瓷砖}公共无效的keyPressed(KeyEvent的E){
如果(isInBound(lastX,lastY)==真){ 的System.out.println(“\ n您走了 网格。\ 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;
尝试{如果(封端(R1,R2)== TRUE) 右= FALSE; //我们不能去 if(blocked(u1,u2)== true)up = false; //如果 (blocking(l1,l2)== true)我们不能上去left = false; //我们不能离开,如果 (封锁(d1,d2)== true)down = false; //我们不能下去
}catch(ArrayIndexOutOfBoundsException
DAP){的System.out.println( “数组索引 是出界...:(\ n” + dap.getCause());}
如果(左== TRUE){如果 (e.getKeyCode()== KeyEvent.VK_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; }}
如果(最多== TRUE){
if (e.getKeyCode() ==
KeyEvent.VK_UP){ 向上= TRUE; py = py-32; lastY = lastY-1; }}
字符串txtLastX = Integer.toString(PX); lbl1.setText(txtLastX);
字符串txtLastY = Integer.toString(PY); lbl2.setText(txtLastY);
字符串txtLastX1 = Integer.toString(lastX); p1.setText(txtLastX1);
字符串txtLastX2 = Integer.toString(lastY); p2。的setText(txtLastX2);重绘();
}公共无效的keyReleased(KeyEvent的 E){
} //忽略公共无效 的keyTyped(KeyEvent的发送){} //忽略
公共无效涂料(图形克){
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; } } //结束涂料方法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; }
公共布尔阻塞(INT TX,TY INT) {返回 BLOCKED_TILES.contains(板[TY] [TX]); }
公共布尔isInBound(INT R,INT C){ 返回(R> = 0)& &(R < 8)& &(C> = 12)& &(C < 1); }
} //结束整个事情
如果这个解决了,这会让我非常不伤心。 :-D我敢肯定,问题出在地图板瓦片...内:\我的猜测......
感谢, 丹
1.给出异常的堆栈跟踪。 2.只给出代码的相关部分 - 即在发生异常的地方附近。 – Bozho 2010-07-10 05:51:33
我随时添加.printStackTrace我得到错误“void is not allowed”或其他什么东西... – nn2 2010-07-10 05:54:36
一堆编程风格狡辩:'if ... inBound()...“你离开电网! ' - 这听起来像是倒退逻辑。如果你处于界限之内,按照惯例的定义,你没有脱离电网。而且,所有'== true'都是不必要的。你可以用if(!respawn)替换'if(right == true)'if'(right)'和'if(respawn == false)'。 – 2010-07-10 05:55:08