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

2014/7/18

java.lang.IndexOutOfBoundsException

MontclairState MontclairState

2014/7/18

#
I'm not really sure what that means. I'm trying to create a scrolling world but I get this error message when I try to compile. The codes used are as follows.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
import java.util.ArrayList;

public class Sky extends World
{
    PlatformMap map = new PlatformMap();
    GreenfootImage mapImg = map.getImage();
    final int MAPINGWIDTH = mapImg.getWidth();
    final int MAPINGHEIGHT = mapImg.getHeight();
    Platform platformTemplate = new Platform(0,0);
    GreenfootImage pfImg = platformTemplate.getImage();
    final int PLATFORMHEIGHT = pfImg.getHeight();
    final int PLATFORMWIDTH = pfImg.getWidth();
    final int MAPWIDTH = MAPINGWIDTH * PLATFORMWIDTH;
    final int MAPHEIGHT = MAPINGHEIGHT * PLATFORMHEIGHT;
    private List<Platform> thePlatforms = new ArrayList<Platform>();
    int leftBound = 0;
    int bottomBound = MAPHEIGHT;
    int topBound = MAPHEIGHT - getHeight();
    int rightBound = getWidth();
    
    public Sky()
    {    
        super(1000, 800, 1); 
        makeMap();
        update();
    }
    public void makeMap()
    {
        for(int y=0; y<MAPHEIGHT; y++)
        {
            for(int x=0; x<MAPWIDTH; x++)
            {
                int colorRGB = mapImg.getColorAt(x,y).getRGB();
                if(colorRGB == Color.BLACK.getRGB())
                {
                    int mapX = x * PLATFORMWIDTH + PLATFORMWIDTH/2;
                    int mapY = y * PLATFORMHEIGHT + PLATFORMHEIGHT/2;
                    thePlatforms.add(new Platform(mapX, mapY));
                   }
            }
        }
    }
        public void update()
        {
           Platform thisPlatform;
           int thisPlatformX;
           int thisPlatformY;
           int screenX;
           int screenY;
           for(int i = 0; i<thePlatforms.size(); i++)
           {
               thisPlatform = thePlatforms.get(i);
               thisPlatformX = thisPlatform.mapX;
               thisPlatformY = thisPlatform.mapY;
               if(thisPlatformX >= leftBound &&
                  thisPlatformX <= rightBound &&
                  thisPlatformY >= topBound && 
                  thisPlatformY <= bottomBound)
                  {
                      screenX = thisPlatformX - leftBound;
                      screenY = thisPlatformY - topBound;
                      if(thisPlatform.getWorld() == null)
                      {
                          addObject(thisPlatform, screenX, screenY);
                      }
                  }
           }
        }
    }
That code is in my world code and I have two actors one is the platforms that will be in my world and the second is the map of the world. The actor with the world map has no code in it and the platforms code is just this.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Platform extends Actor
{
    int mapX;
    int mapY;
    
    public Platform(int getMapX, int getMapY)
    {
        mapX = getMapX;
        mapY = getMapY;
    }
}
I'm not sure what the problems is it keeps pointing to this code when it gives the error.
int colorRGB = mapImg.getColorAt(x,y).getRGB();
danpost danpost

2014/7/18

#
Should not lines 32 and 34 be using MAPIMGWIDTH and MAPIMGHEIGHT and not MAPWIDTH and MAPHEIGHT?
MontclairState MontclairState

2014/7/18

#
Wow...thanks danpost. That was it.
rikd1977 rikd1977

2014/7/22

#
thank you so much!! i had the same exact problem!! i wonder if jim may have had that there and forgot to change? i tried to contact him but he hasnt been on his youtube??? again thanks and im sure ill be back!! this is a nice ide
NikZ NikZ

2014/7/24

#
IndexOutOfBounds just means you are calling a part of a list not in the list. For example: Your list of integers is 1, 3, 4, 7, 8. Then you call the tenth integer in the list. There is none, so it prints out an error.
You need to login to post a reply.