2010-05-27 122 views
0

問題是我試圖讓某些圖塊被阻塞,所以玩家不能在它們上面行走。但是,它只是讀取第一塊瓷磚,這是board[0][0],其他一切都沒有檢查....Java for-loop問題

我做錯了什麼? :(

這裏是我的代碼:

import java.applet.*; 
import java.applet.Applet; 

import java.awt.*; 
import java.awt.Canvas.*; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.*; 


public class gen extends Applet implements KeyListener { 
    Image[] tiles; 
    Image player; 
    int x; 
    int y; 
    int px; 
    int py; 
    boolean left; 
    boolean right; 
    boolean down; 
    boolean up; 
    int[][] board; 
    final int NUM_TILES = 5; 
    public final int BLOCKED = 1; 



    public void init() { 
    // Load board 
    board = loadBoard(); 





     tiles = new Image[NUM_TILES]; 
    for(int i = 0;i < NUM_TILES;i++) { 
     tiles[i] = getImage(getClass().getResource(String.format("tile%d.png", i))); 
    } 
    for (int y=0;y< NUM_TILES;y++) { 
     board[1][1] = BLOCKED; 
     board[1][2] = BLOCKED; 
     board[1][3] = BLOCKED; 
     } 
     player = getImage(getClass().getResource("player.png")); // our player 
     addKeyListener(this); 

     px = 0; 

     py = 0; 
    } 

    public void keyPressed(KeyEvent e) { 



     if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
      left = true; 

      px = px - 32; 
     } 

     if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
      right = true; 

      px = px + 32; 
     } 

     if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
      down = true; 

      py = py + 32; 
     } 

     if (e.getKeyCode() == KeyEvent.VK_UP) { 
      up = true; 

      py = py - 32; 
     } 

     repaint(); 

    } 

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

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

    public void paint(Graphics g) { 
     x = 0; 

     y = 0; 

     // here's a sample map! 

     // but we're loading from a text file! 

     // so... why is this needed? 
     for (int row = 0; row < board.length; row++) { 
      for (int col = 0; col < board[row].length; col++) { 
       int index = board[row][col]; 
       g.drawImage(tiles[index], 32 * col, 32 * row, this); 
       if (board[row][col] == BLOCKED) { 
       System.out.println("\n"+board[row][col] + "is BLOCKED!\n"); 
       }else{System.out.println("\n"+board[row][col] + "is NOT Blocked!\n");} 
      } 
     } 

     g.drawImage(player, px, py, this); 
     try { 
     System.out.println("ON BLOCKED TILE?: " + blocked(px,py) + "\n"); 
    }catch(ArrayIndexOutOfBoundsException e) {} 
    } // end paint method 

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

    private int[][] loadBoard() { 
     int[][] board = { 
       { 0, 1, 2, 3, 4, 4, 3, 4 }, 
       { 0, 1, 2, 3, 4, 4, 3, 4 }, 
       { 2, 2, 4, 2, 2, 1, 1, 0 }, 
       { 0, 1, 2, 3, 4, 4, 3, 4 }, 
       { 0, 0, 0, 2, 3, 4, 4, 2 }, 
       { 2, 2, 4, 2, 2, 1, 1, 0 }, 
       { 0, 1, 2, 3, 4, 4, 3, 4 }, 
       { 0, 0, 0, 2, 3, 4, 4, 2 } 
      }; 
    return board; 
    } 

    public boolean blocked(int tx, int ty) { 
      return board[tx][ty] == BLOCKED; 
     } 
} // end whole thing 
+0

您能否將代碼添加到您的問題? – 2010-05-27 18:11:12

+1

你在說這個循環嗎?對於(int y = 0; y RoflcoptrException 2010-05-27 18:11:25

+0

我有一個循環,因爲它是板[1] [Y]所以這將是一整行或coloumn將被阻止,但後來我遇到了這個問題。 – nn2 2010-05-27 18:22:23

回答

1

一個在你的代碼中的問題是在這條線,你在呼喚封閉PX,PY這是32的倍數:

blocked(px, py) 

但您使用這些數字作爲指標到您的陣列,這將導致一個ArrayIndexOutOfBoundsException

public boolean blocked(int tx, int ty) 
{ 
    return board[tx][ty] == BLOCKED; 
} 

其中Y ou've試圖忽略它「修復」:

try 
{ 
    System.out.println("ON BLOCKED TILE?: " + blocked(px,py) + "\n"); 
} 
catch(ArrayIndexOutOfBoundsException e) {} 

所以我懷疑它僅適用於(0,0),因爲(32,32)是出界。您的代碼中還存在其他錯誤,但這對您而言應該是一個好的開始。

+0

哦,哇。我現在看到問題了!謝謝!而且...我的代碼上沒有看到任何其他錯誤。 :S – nn2 2010-05-27 18:21:21