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

2017/10/28

Im getting an error from the world constructor when I try to compile

darkstargamer darkstargamer

2017/10/28

#
I'm trying to make a java version of super Mario bros, and I'm working on spawning the terrain. I can't compile without the world constructor throwing an exception. I used Mr. Stewart's 1st lesson on making a platform game, which involved constructing platforms automatically from a gif file, with black pixels representing platforms. the image i'm using for the map is 193x13 pixels, the groundblock image is 32x32 pixels, and the sky image is 512x512 pixels. here's the code I used for the World (the sky):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import greenfoot.*;
import java.awt.Color;
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Sky here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Sky extends World
{
    GroundBlockMap map = new GroundBlockMap();
    GreenfootImage mapImg = map.getImage();
    final int MAPIMGWIDTH = mapImg.getWidth();
    final int MAPIMGHEIGHT = mapImg.getHeight();
    GroundBlock groundBlockTemplate = new GroundBlock(0,0);
    GreenfootImage pfImg = groundBlockTemplate.getImage();
    final int PLATFORMHEIGHT = pfImg.getHeight();
    final int PLATFORMWIDTH = pfImg.getWidth();
    final int MAPWIDTH = MAPIMGWIDTH * PLATFORMWIDTH;
    final int MAPHEIGHT = MAPIMGHEIGHT * PLATFORMHEIGHT;
    private List<GroundBlock> theGroundBlocks = new ArrayList<GroundBlock>();
    int leftBound = 0;
    int bottomBound = MAPHEIGHT;
    int topBound = MAPHEIGHT - getHeight();
    int rightBound = getWidth();
    /**
     * Constructor for objects of class Sky.
     *
     */
    public Sky()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(512, 512, 1);
        makeMap();
        update();
    }
 
    public void makeMap()
    {
        for(int y=0;y<MAPIMGHEIGHT;y++)
        {
            for(int x=0;x<MAPIMGWIDTH;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;
                    theGroundBlocks.add(new GroundBlock (mapX, mapY));
                }
            }
        }
    }
     
    public void update()
    {
        GroundBlock thisGroundBlock;
        int thisGroundBlockX;
        int thisGroundBlockY;
        int screenX;
        int screenY;
        for(int i=0; 1<theGroundBlocks.size(); i++)
        {
            thisGroundBlock = theGroundBlocks.get(i);
            thisGroundBlockX = thisGroundBlock.mapX;
            thisGroundBlockY = thisGroundBlock.mapY;
            if(thisGroundBlockX>=leftBound && thisGroundBlockX<=rightBound && thisGroundBlockY>=topBound && thisGroundBlockY<=bottomBound)
             
            {
                screenX = thisGroundBlockX - leftBound;
                screenY = thisGroundBlockY - topBound;
                if(thisGroundBlock.getWorld()==null)
                {
                    addObject (thisGroundBlock, screenX, screenY);
                }
            }
        }
    }
}
Heres code for the GroundBlock class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;
 
/**
 * Write a description of class GroundBlock here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class GroundBlock extends Actor
{
    int mapX;
    int mapY;
     
    public GroundBlock(int getMapX, int getMapY)
    {
        mapX = getMapX;
        mapY = getMapY;
    }
}
there is no code aside from the default code for the GroundBlockMap. (which extends actor) finally, here is the error (notice: I didn't know how to embed the image I took on the computer onto here, so i'm copy-pasting) to clarify once more, the world constructor threw this error at me. I'll underline the parts of the text that were in red on the error message. java.lang.IndexOutOfBoundsException: Index: 372, Size: 372 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at Sky.update(Sky.java:66) at Sky.<init>(Sky.java:37) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at greenfoot.core.Simulation.newInstance(Simulation.java:607) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:441) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:494) at greenfoot.core.Simulation.maybePause(Simulation.java:299) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2017/10/28

#
At line 64, you have a '1' (one) where an 'i' (eye) should be.
darkstargamer darkstargamer

2017/10/28

#
I feel so stupid, thank you for that!
danpost danpost

2017/10/28

#
darkstargamer wrote...
I feel so stupid, thank you for that!
lol. I had no intention of making you feel stupid (I know that is not what you meant -- you're welcome). Actually, you should not feel that way as the simplest errors can sometimes be the hardest to find.
darkstargamer darkstargamer

2017/10/29

#
Thanks. also, how do you get colors like orange, purple, and yellow to be recognized by greenfoot?
danpost danpost

2017/10/29

#
Yellow and orange should just be 'Color.YELLOW' and 'Color.ORANGE'. There is no preset value for purple; however, you can create the color using 'new Color(127, 0, 127)'.
darkstargamer darkstargamer

2017/10/29

#
thank you!
You need to login to post a reply.