Error java.lang.ArrayIndexOutOfBoundsException at Eclipse

-3

i'm a newbie in java and im having a error at Eclipse java, i cant explain what is going on (its a little troublesome), so please someone help me, the only thing i know is that the erro cames after i typed the last yfinal at render (at the las for int in the code), the error is:

Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index -86 out of bounds for length 400
    at com.FsStudios.World.World.render(World.java:78)
    at com.FsStudios.main.Game.render(Game.java:106)
    at com.FsStudios.main.Game.run(Game.java:134)
    at java.base/java.lang.Thread.run(Thread.java:832)

Here is the code:


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

import javax.imageio.ImageIO;

import com.FsStudios.entities.Bullet;
import com.FsStudios.entities.Enemy;
import com.FsStudios.entities.Entity;
import com.FsStudios.entities.LifePack;
import com.FsStudios.entities.Weapon;
import com.FsStudios.main.Game;

public class World {
    
    private Tile[] tiles;
    public static int WIDTH, HEIGHT;
    
    
    public World(String path) {
    
        try {
            BufferedImage map = ImageIO.read(getClass().getResource(path));
            int pixels[] = new int[map.getWidth() * map.getHeight()]; 
            WIDTH = map.getWidth();
            HEIGHT = map.getHeight();
            
            tiles = new Tile[map.getWidth() * map.getHeight()];
            map.getRGB(0, 0, map.getWidth(), map.getHeight(), pixels, 0, map.getWidth());
            for(int xx = 0; xx < map.getWidth(); xx++) {
                for(int yy = 0; yy < map.getHeight(); yy++) {
                    int pixelAtual = pixels[xx + (yy*map.getWidth())];
                    tiles[xx + (yy * map.getWidth())] = new FloorTile(xx*16 , yy*16, Tile.TILE_FLOOR);
                    if(pixelAtual == 0xFF000000) {
                        //Floor
                        tiles[xx + (yy * map.getWidth())] = new FloorTile(xx*16 , yy*16, Tile.TILE_FLOOR);
                    }else if(pixelAtual == 0xFFFFFFFF) {
                        //wall
                        tiles[xx + (yy * map.getWidth())] = new WallTile(xx*16 , yy*16, Tile.TILE_WALL);
                    }else if(pixelAtual == 0xFF0000FF) {
                        //player
                        Game.player.setX(xx*16);
                        Game.player.setY(yy*16); 
                    }else if(pixelAtual == 0xFFFF0000) {
                        //enemy
                        Game.entities.add(new Enemy(xx*16, yy*16, 16, 16, Entity.ENEMY_ENT));
                    }else if(pixelAtual == 0xFFFF6A00) {
                        //Weapon
                        Game.entities.add(new Weapon(xx*16, yy*16, 16, 16, Entity.WEAPON_ENT));
                    }else if(pixelAtual == 0xFFFF8989) {
                        //LifePack
                        Game.entities.add(new LifePack(xx*16, yy*16, 16, 16, Entity.LIFEPACK_ENT));
                    }else if(pixelAtual == 0xFFFFD800) {
                        //Bullet
                        Game.entities.add(new Bullet(xx*16, yy*16, 16, 16, Entity.BULLET_ENT));
                    }
                
                    
                }
            }
                
        
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
       public void render(Graphics g) {
           int xstart = Camera.x/16;
           int ystart = Camera.y/16;
           
           int xfinal = xstart + (Game.WIDTH / 16);
           int yfinal = ystart + (Game.HEIGHT / 16);
           
           for(int xx = xstart; xx <= xfinal; xx++) {
                for(int yy = ystart; yy <= yfinal; yy++) {
                    Tile tile = tiles[xx + (yy*WIDTH)];
                    tile.render(g);
                }
           }
           
       }
}

if theres someone who can answer i would be thankfull.

java
asked on Stack Overflow Jan 23, 2021 by Fskubs Arts • edited Jan 23, 2021 by Fskubs Arts

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0