2013-10-09 54 views
0

我一直在通過YouTube教程開發一款遊戲,有些人可能會認爲它是由designsbyzephr提供的,並且遇到問題。我確信代碼是正確的,因爲我之前在之前的項目中使用過這個調用。即使使用在github上發佈的完整項目源代碼,我也會遇到數組越界的異常。有什麼建議麼?陣列越界錯誤

package Game; 

import java.awt.BorderLayout; 
import java.awt.Canvas; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 
import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferInt; 

import javax.swing.JFrame; 

import GFX.Screen; 
import GFX.SpriteSheet; 

public class Game extends Canvas implements Runnable { 
private static final long serialVersionUID = 1L; 

public static final int WIDTH = 160; 
public static final int HEIGHT = WIDTH/12 * 9; 
public static final int SCALE = 3; 

public static final String NAME = "Game"; 

private JFrame frame; 

public boolean running = false; 
public int tickcount = 0; 

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); 

private Screen screen; 

public Game() { 
    setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
    setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 

    frame = new JFrame(NAME); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(new BorderLayout()); 

    frame.add(this, BorderLayout.CENTER); 
    frame.pack(); 

    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 

public void init() { 
    screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/Sprites/spritesheet.png")); 
} 

public void render() { 
    BufferStrategy bs = getBufferStrategy(); 
    if (bs == null) { 
     createBufferStrategy(3); 
     return; 
    } 
    screen.render(pixels, 0, WIDTH); 

    Graphics g = bs.getDrawGraphics(); 
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 

    g.dispose(); 
    bs.show(); 
} 

public synchronized void start() { 
    running = true; 
    new Thread(this).start(); 
} 

public synchronized void stop() { 
    running = false; 
} 

public void run() { 
    long lastTime = System.nanoTime(); 
    double nsPerTick = 1000000000D/60D; 

    int ticks = 0; 
    int frames = 0; 

    long lastTimer = System.currentTimeMillis(); 
    double delta = 0; 

    init(); 

    while (running) { 
     long now = System.nanoTime(); 
     delta += (now - lastTime)/nsPerTick; 
     lastTime = now; 
     boolean shouldRender = true; 

     while (delta >= 1) { 
      ticks++; 
      tick(); 
      delta -= 1; 
      shouldRender = true; 
     } 

     try { 
      Thread.sleep(2); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     if (shouldRender) { 
      frames++; 
      render(); 
     } 

     if (System.currentTimeMillis() - lastTimer >= 1000) { 
      lastTimer += 1000; 
      frames = 0; 
      ticks = 0; 
     } 
    } 
} 

public void tick() { 
    tickcount++; 

    for (int i = 0; i < pixels.length; i++) 
     pixels[i] = i + tickcount; 
} 

public static void main(String[] args) { 
    new Game().start(); 
} 

}

package GFX; 

    import java.awt.image.BufferedImage; 
    import java.io.IOException; 

    import javax.imageio.ImageIO; 


    public class SpriteSheet { 

public String path; 
public int width; 
public int height; 

public int[] pixels; 

public SpriteSheet(String path) { 
    BufferedImage image = null; 

    try 
    { 
     image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path)); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

    if (image == null) return; 

    this.path = path; 
    this.width = image.getWidth(); 
    this.height = image.getHeight(); 

    pixels = image.getRGB(0, 0, width, height, null, 0, width); 

    for (int i = 0; i < pixels.length; i++) { 
     pixels[i] = (pixels[i] & 0xff)/64; 
    } 
    for (int i = 0; i < 8; i++) 
     System.out.println(pixels[i]); 
} 

}

package GFX; 

    public class Screen { 
public static final int MAP_WIDTH = 64; 
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1; 

public int[] tiles = new int[MAP_WIDTH * MAP_WIDTH]; 
public int[] colors = new int[MAP_WIDTH * MAP_WIDTH]; 

public int xOffset = 0; 
public int yOffset = 0; 

public int width; 
public int height; 

public SpriteSheet sheet; 

public Screen(int width, int height, SpriteSheet sheet) { 
    this.width = width; 
    this.height = height; 
    this.sheet = sheet; 

    for (int i = 0; i < MAP_WIDTH * MAP_WIDTH; i++) { 
     colors[i * 4 + 0] = 0xff00ff; 
     colors[i * 4 + 1] = 0x00ffff; 
     colors[i * 4 + 2] = 0xffff00; 
     colors[i * 4 + 3] = 0x0000ff; 
    } 
} 

public void render(int[] pixels, int offset, int row) { 
    for (int yTile = yOffset >> 3; yTile <= (yOffset + height) >> 3; yTile++) { 
     int yMin = yTile * 8 - yOffset; 
     int yMax = yMin + 8; 
     if (yMin < 0) 
      yMin = 0; 
     if (yMin > height) 
      yMax = height; 

     for (int xTile = xOffset >> 3; xTile <= (xOffset + width) >> 3; xTile++) { 
      int xMin = xTile * 8 - xOffset; 
      int xMax = xMin + 8; 
      if (xMin < 0) 
       xMin = 0; 
      if (xMin > width) 
       xMax = width; 

      int tileIndex = (xTile & (MAP_WIDTH_MASK)) 
        + (yTile & (MAP_WIDTH_MASK)) * MAP_WIDTH; 

      for (int y = yMin; y < yMax; y++) { 
       int sheetPixel = ((y + yOffset) & 7) * sheet.width 
         + ((xMin + xOffset) & 7); 
       int tilePixel = offset + xMin + y * row; 

       for (int x = xMin; x < xMax; x++) { 
        int color = tileIndex * 4 + sheet.pixels[sheetPixel++]; 
        pixels[tilePixel++] = colors[color]; 
       } 
      } 
     } 
    } 
} 

}

這是所有的代碼爲止。錯誤如下 線程「Thread-2」中的異常java.lang.IllegalArgumentException:input == null! (Game.Game.run)上的Game.Game.init(Game.java:54) (在SpySheet.java:22) 。 Java的:91) 在java.lang.Thread.run(來源不明)

+1

您可以刪除與您的問題無關的所有不相關代碼,並指出錯誤語句指向的具體行號爲 – Prateek

+0

它是一個'ArrayOutOfBounds'異常或一個'IllegalArgumentException'你有麻煩嗎? – vandale

+0

這不是一個數組越界的錯誤。 –

回答

2

正是在這一行

image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path)); 

你的程式,它返回空,空指針異常被拋出找不到文件當您嘗試從零讀取

您需要確保有一個文件「/Sprites/spritesheet.png」相對於您正在運行程序的位置

+0

如果它與程序的位置有關,它不應以「/」開頭。 –