ArrayIndexOutOfBounds--Making a tilemap
in
Programming Questions
•
3 years ago
I'm creating a side scrolling platform game, and the first step was to see if I could make the map. I have an array of integers to represent my tiles, and then I loop through my image array to load the actual images. Then, i would loop through the array to place each tile on the screen. I'm not sure why, but i keep get an arrayOutofBounds error, but I checked to make sure that the for loop was correct, and I'm not understanding the error. I posted the code below, except for the TileMap class and I am hoping someone can tell me what the problem is.
PImage bg;
TileMap map;
void setup()
{
size(3200,640);
map= new TileMap(100,20);
bg=loadImage("level_1_background.png");
for(int i=0;i<20;i++)
{
for(int j=0;j<100;j++)
{
if(tile_map[j][i]==0)
map.setTile(loadImage("blank.gif"),j,i);
if(tile_map[j][i]==1)
map.setTile(loadImage("tree_1.gif"),j,i);
if(tile_map[j][i]==2)
map.setTile(loadImage("tree_2.gif"),j,i);
if(tile_map[j][i]==3)
map.setTile(loadImage("tree_3.gif"),j,i);
if(tile_map[j][i]==4)
map.setTile(loadImage("tree_4.gif"),j,i);
if(tile_map[j][i]==5)
map.setTile(loadImage("tree_5.gif"),j,i);
if(tile_map[j][i]==6)
map.setTile(loadImage("tree_6.gif"),j,i);
if(tile_map[j][i]==7)
map.setTile(loadImage("tree_7.gif"),j,i);
if(tile_map[j][i]==8)
map.setTile(loadImage("tree_8.gif"),j,i);
if(tile_map[j][i]==9)
map.setTile(loadImage("tree_9.gif"),j,i);
if(tile_map[j][i]==10)
map.setTile(loadImage("tree_10.gif"),j,i);
if(tile_map[j][i]==11)
map.setTile(loadImage("tree_11.gif"),j,i);
if(tile_map[j][i]==12)
map.setTile(loadImage("tree_12.gif"),j,i);
if(tile_map[j][i]==13)
map.setTile(loadImage("tree_13.gif"),j,i);
if(tile_map[j][i]==14)
map.setTile(loadImage("tree_14.gif"),j,i);
if(tile_map[j][i]==15)
map.setTile(loadImage("tree_15.gif"),j,i);
if(tile_map[j][i]==16)
map.setTile(loadImage("tree_16.gif"),j,i);
if(tile_map[j][i]==17)
map.setTile(loadImage("tree_17.gif"),j,i);
if(tile_map[j][i]==18)
map.setTile(loadImage("tree_18.gif"),j,i);
if(tile_map[j][i]==19)
map.setTile(loadImage("tree_19.gif"),j,i);
if(tile_map[j][i]==20)
map.setTile(loadImage("tree_20.gif"),j,i);
if(tile_map[j][i]==21)
map.setTile(loadImage("tree_21.gif"),j,i);
if(tile_map[j][i]==22)
map.setTile(loadImage("tree_22.gif"),j,i);
if(tile_map[j][i]==23)
map.setTile(loadImage("tree_23.gif"),j,i);
if(tile_map[j][i]==24)
map.setTile(loadImage("tree_24.gif"),j,i);
if(tile_map[j][i]==25)
map.setTile(loadImage("tree_25.gif"),j,i);
if(tile_map[j][i]==26)
map.setTile(loadImage("tree_26.gif"),j,i);
if(tile_map[j][i]==27)
map.setTile(loadImage("tree_27.gif"),j,i);
if(tile_map[j][i]==28)
map.setTile(loadImage("tree_28.gif"),j,i);
if(tile_map[j][i]==29)
map.setTile(loadImage("tree_29.gif"),j,i);
if(tile_map[j][i]==31)
map.setTile(loadImage("Platform1.png"),j,i);
if(tile_map[j][i]==32)
map.setTile(loadImage("platform2.png"),j,i);
if(tile_map[j][i]==33)
map.setTile(loadImage("platform3.png"),j,i);
if(tile_map[j][i]==34)
map.setTile(loadImage("tree_bush.gif"),j,i);
if(tile_map[j][i]==35)
map.setTile(loadImage("rock 1.png"),j,i);
if(tile_map[j][i]==36)
map.setTile(loadImage("rock2.png"),j,i);
if(tile_map[j][i]==37)
map.setTile(loadImage("rock3.png"),j,i);
if(tile_map[j][i]==38)
map.setTile(loadImage("rock6.png"),j,i);
if(tile_map[j][i]==39)
map.setTile(loadImage("bridge_1.png"),j,i);
if(tile_map[j][i]==40)
map.setTile(loadImage("bridge_2.png"),j,i);
if(tile_map[j][i]==41)
map.setTile(loadImage("bridge_3.png"),j,i);
}
}
}
void draw()
{
image(bg,0,0);
for(int i=0; i< map.getHeight();i++)
{
for(int j=0;j< map.getWidth();j++)
{
image(map.getTile(j,i),gpft(j),gpft(i));
}
}
}
int gpft(int t) //get pixels from tiles
{
return t*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,0,0,0,0,0,0,34,34,0,0,0,0,0,
0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,23,24,25,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23,24,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,32,0,35,37,38,0,0,0,0,35,
36,36,36,36,36,36,36,36,36,38,0,0,0,
0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,
32,33,0
},
{0,0,0,0,0,0,0,34,34,0,0,34,0,26,27,28,
29,34,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,26,27,28,29,34,0,0,0,0,0,0,0,34,34,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
35,36,36,36,36,36,36,36,38,0,0,0,0,0,0,
0,0,0,0,0,0,36,36,36,36,36,36,36,36,38,
0
},
{0,31,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,33,0,0,0,0,0,31,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,0,
0,0,0,32,32,33,0,0,0,0,0,0,0,0,0,0,0,
35,36,36,36,37,37,38,0,0,0,0,0,0,0,0,
0,34,34,0,0,36,36,36,36,36,36,36,38,0,
0
},
{0,35,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,38,0,0,0,0,0,35,36,36,
36,36,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,36,36,36,36,36,36,36,0,
0,0,0,36,36,38,0,31,32,33,0,0,0,0,0,0,
0,0,37,37,37,0,0,0,0,0,0,0,0,0,0,32,
32,32,32,32,32,32,32,33,37,37,37,37,0,
0,0
},
{0,0,35,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,38,0,0,0,31,33,0,0,35,36,
36,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,36,36,36,36,36,36,36,
36,32,32,32,32,36,38,0,0,36,36,36,33,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,36,36,36,36,36,36,36,36,38,0,0,
0,0,0,0,0
},
{0,0,0,35,37,37,37,36,36,36,37,37,37,
37,37,37,38,0,0,0,0,37,37,0,0,0,35,
36,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,38,0,0,0,36,36,36,36,
32,33,0,0,0,0,39,40,40,40,40,41,0,0,
0,0,0,0,0,34,36,36,36,36,36,36,36,38,
0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,37,37,37,0,0,0,0,0,0,0,
34,0,0,0,0,0,0,0,0,0,35,37,35,36,36,
36,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,36,36,36,36,36,36,38,
0,0,0,0,35,36,36,36,36,36,32,32,32,
32,0,0,0,0,0,0,32,32,32,32,32,32,32,
32,33,37,36,36,37,37,37,0,0,0,0,0,0,
0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,31,
32,33,0,0,0,0,0,0,0,0,0,0,35,37,37,37,
37,37,37,37,37,37,37,37,37,37,37,37,
37,37,37,37,37,37,37,37,38,0,0,0,0,0,
0,35,36,36,36,36,36,36,36,36,0,0,0,0,
0,0,35,36,36,36,36,36,36,36,38,0,37,
37,0,0,0,0,0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,37,
37,37,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,37,37,36,36,36,36,36,
37,0,0,0,0,0,0,0,35,36,36,36,36,
36,38,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,31,32,32,33,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0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