This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nooyi
Nooyi wrote ...

2014/1/6

array has been accessed with illegal index?

Nooyi Nooyi

2014/1/6

#
Hello, I am creating a little game, the world is 15x12 big with a pixsize of 60. I want to use this multidimensional array, it's like a plan for my level:
private int [][] lvlData = {
        { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
        { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
    };
and I am using this code here to place the actors (0 == nothing, 1 == Tree):
 public void init_map() 
    {    
        for (int y = 0; y < getHeight() ; y++)   {

            for(int x = 0; x < getWidth(); x++)     {

                    int ID = lvlData[x][y];

                    if (ID == 1) 
                    {
                        addObject(new Tree(),x,y);
                    }
            }
        }
    }    
And everytime I compiled it, everything was ok. But if I click at the start button, the game stops and this appears: java.lang.ArrayIndexOutOfBoundsException: 12
Nooyi Nooyi

2014/1/6

#
oh and I googled a bit and than found out that this means that the array has been accessed with illegal index, but I don't know how to fix it. Is there anyone who could help me? :)
bourne bourne

2014/1/6

#
instead of: y < getHeight() do this: y < lvlData.length And instead of : x < getWidth() do this: x < levelData.length (based on how you access lvlData: lvlData)
Nooyi Nooyi

2014/1/6

#
Thanks so much for your help, bourne, now it's working! :D
You need to login to post a reply.