Hello! I was following a tutorial on how to make a game when i suddenly got an error concerning moving tiles. i get this error when i tried to use a and d to move:
Exception in thread "Display" java.lang.ArrayIndexOutOfBoundsException: 48600
at com.cherno.graphics.Screen.render(Screen.java:44)
at com.cherno.Game.render(Game.java:124)
at com.cherno.Game.run(Game.java:87)
at java.lang.Thread.run(Unknown Source)
I do not get the error while using w or s but the tiles are still not moving when pressing them.
I have three classes at work here.
The main, Game.java;
package com.cherno; import java.awt.Canvas; import java.awt.Color; 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 com.cherno.Game; import com.cherno.graphics.Screen; import com.cherno.input.Keyboard; public class Game extends Canvas implements Runnable{ private static final long serialVersionUID = 1L; public static int width = 300; public static int height = width / 16 * 9; public static int scale = 3; private Thread thread; private JFrame frame; private Keyboard key; private boolean running = false; private Screen screen; private BufferedImage image= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); public Game(){ Dimension size = new Dimension(width * scale, height * scale); setPreferredSize(size); screen = new Screen(width, height); frame = new JFrame(); key = new Keyboard(); addKeyListener(key); } public synchronized void start(){ running = true; thread = new Thread(this, "Display"); thread.start(); } public synchronized void stop(){ running = false; try{ thread.join(); }catch(InterruptedException e){ e.printStackTrace(); } } public void run(){ long lastTime = System.nanoTime(); long timer = System.currentTimeMillis(); final double ns= 1000000000.0 / 60.0; double delta = 0; int frames = 0; int updates = 0; while (running){ long now = System.nanoTime(); delta=delta+=(now-lastTime)/ns; lastTime = now; while (delta >= 1){ update(); updates++; delta--; } render(); frames++; if (System.currentTimeMillis() - timer > 1000){ timer += 1000; //System.out.println(updates + "ups, " + frames + " fps"); frame.setTitle("false rabbit " + " | "+updates + "ups, " + frames + " fps"); updates=0; frames=0; } } stop(); } public int x=0, y=0; public void update(){ key.update(); if(key.up)y--; if(key.down)y++; if(key.left)x--; if(key.right)x++; } public void render(){ BufferStrategy bs = getBufferStrategy(); if (bs==null){ createBufferStrategy(3); return; } screen.clear(); screen.render(x, 0); for (int i = 0; i < pixels.length; i++){ pixels[i] = screen.pixels[i]; } Graphics g = bs.getDrawGraphics(); g.drawImage(image, 0, 0, getWidth(), getHeight(), null); g.dispose(); bs.show(); } public static void main(String[] args){ Game game = new Game(); game.frame.setResizable(false); //game.frame.setTitle("rabbit false"); game.frame.add(game); game.frame.pack(); game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.frame.setLocationRelativeTo(null); game.frame.setVisible(true); game.start(); } }
The class that mainly structures the "screen", Screen;
package com.cherno.graphics; import java.util.Random; public class Screen { private int width, height; public int[] pixels; public final int MAP_SIZE = 64; public final int MAP_SIZE_MASK = MAP_SIZE -1; public int[] tiles = new int[MAP_SIZE * MAP_SIZE]; private Random random = new Random(); public Screen(int width, int height){ this.width = width; this.height = height; pixels = new int[width * height]; for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++){ tiles[i] = random.nextInt(0xffffff); } } public void clear(){ for (int i = 0; i < pixels.length; i++){ pixels[i] = 0; } } public void render(int xoffset, int yoffset){ for(int y = 0; y < height; y++){ int yy = y + yoffset; //if (yy < 0 || yy >= height )break; for(int x = 0; x < width; x++){ int xx = x + xoffset; //if (xx < 0 || xx >= width )break; int tileIndex = ((xx >> 4) & MAP_SIZE_MASK ) + ((yy >> 4) & MAP_SIZE_MASK) * MAP_SIZE; pixels[xx + yy * width] = tiles[tileIndex]; //error uccurs when using a and d not w or s } } } }
And at last The Key class that listens to my keystrokes, i present..
Keyboard;
package com.cherno.input; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Keyboard implements KeyListener{ private boolean[] keys = new boolean[120]; public boolean up, down, left, right; public void update(){ up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W]; down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S]; left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A]; right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D]; } public void keyPressed(KeyEvent e) { keys[e.getKeyCode()] = true; } public void keyReleased(KeyEvent e) { keys[e.getKeyCode()] = false; } public void keyTyped(KeyEvent e) { } }
I am SO happy for answers!