ArrayOutOfBounds... but why?
in
Programming Questions
•
3 years ago
Hello everyone! I'm new new new to processing.
I am trying to write a program that blacks out an image as part of a shooter-type game. Kinda like block-out meets space invaders. The way I am doing this is trying to draw a black rectangle wherever the bullet hits.
I keep getting a weird ArrayOutOfBounds Exception problem ever since I started trying to code for the blackened-out rectangles... Maybe you could help?
Thanks in advance!!!
I am trying to write a program that blacks out an image as part of a shooter-type game. Kinda like block-out meets space invaders. The way I am doing this is trying to draw a black rectangle wherever the bullet hits.
I keep getting a weird ArrayOutOfBounds Exception problem ever since I started trying to code for the blackened-out rectangles... Maybe you could help?
Thanks in advance!!!
- /*
Bree Rubin's quarter of the destruction project.
A lot of this code was helped by attending the workshop
wit Ramsey and Eric on making Asteroids.
--- got more help later *sigh*
*/
//laser gun == RADICAL!!!
float laserX, laserY, laserSpeed, startPoint;
//shooter
float zapX, zapY, zapSpeed;
boolean zapVis; //whether or not the zap exists
//Key input
int keyDownArraySize = 127;
boolean[] keyDown = new boolean [keyDownArraySize]; //not 100% on this one
//bgimg
PImage zeImg;
//grid sectors
int sqWidth, sqHeight;
//rows cols for grid
int rows, cols;
int i, g;
//array visibility of blackbox
boolean[][] sqVis = new boolean[rows][cols];
int[][] sqAlpha = new int[rows][cols];
////////////////////////BEGIN SETUP --------------
void setup(){
// frameRate(20);
size(1024 , 768);
zeImg = loadImage("logo_bottomleft.jpg");
smooth();
rows = 12;
cols = 12;
sqWidth = width/cols;
sqHeight = height/rows;
initLaser();
initZap ();
}//end setup
////////////////////////BEGIN INITIALIZING STUFF-------
void initLaser (){
laserX = 0;
laserY = 0;
laserSpeed = 5;
startPoint = height-100;
}
void initZap (){
zapX = 0;
zapY = 0;
zapSpeed = 1;
zapVis = false;
}
/////////////////////BEGIN DRAW LOOP ------
void draw() {
//background(0);
image(zeImg, 0, 0);
drawLaser();
updateLaser();
if (zapVis) {
drawZap();
updateZap();
}
inputHandler();
hitDetect();
}//end draw loop
////////////////////OBJECTS
void drawLaser() {
stroke(255);
fill(0,255,0);
pushMatrix();
//christmas
//height-200 should also be a variable like... bottomStart or something
translate(laserX, startPoint);
rect(0,0,60,10);
popMatrix(); //end matrix
} // end drawLaser
void updateLaser() {
if ( laserX+50 > width) {
laserX = width-50;
}
if ( laserX < 0 ) {
laserX = 0;
}
}// end updateLaser
void drawZap() {
noStroke();
fill(random(0,255),random(0,255),random(0,255));
pushMatrix();
//christmas
//make the 40 a variable, laserWidth, 30 is half what the width is right now
translate(laserX+30, height-200);
ellipse(0,zapY,10,50);
popMatrix(); //end matrix
} // end drawLaser
void updateZap() {
//christmas
//zapx = laserX-50 didn't seem to do anything, deleted, it's been put into the translate()
//of drawZap()
//should prolly put the 10 as a zapSpeed variable
zapY-=10;
//christmas
//it's doing something strange from the translation, i'm not sure why but it works
//you will probably want to dbl check what it is later... zapY is 0 when it is fired
//and goes negative, that's why it's -(height-200)
if (zapY < -(height-200)) {
zapVis = false;
}
}// end updatezap
////////////////////OBJECT BEHAVIORS
void hitDetect (){
for (int i=0; i<rows; i++){
for (int g=0; g<cols; g++){
sqVis[i][g] = false;
if (zapX >= i && zapX <= (i+sqWidth) && zapY >=g && zapY <= (g+sqHeight)){
sqVis[i][g] = true;
}
if (!sqVis[i][g]){
println("FALSE");
}
else if (sqVis[i][g]){
println("tru");
rect(i,g,(i+sqWidth), (g+sqHeight));
sqAlpha[i][g]--; //decreases alpha
}
println(g);
println(i);
}//end g for
}//end i for
}
/////////////////////INPUT HANDLERS ETC
void inputHandler() {
// begin controlling my laserz
//christmas
//fixed your left right stuff
if (keyDown[LEFT] || keyDown[37]) {
laserX-=5;
}
if (keyDown[RIGHT] || keyDown[39]) {
laserX+=5;
}
//space is pressed to fire ze laserz!!!
if (keyDown[32]) {
if(!zapVis) {
//christmas
//it's not resetting the values of the bullet so i call initZap() here to clear
//the previous bullet's coords
//deleted the extraneous code
initZap();
zapVis = true;
}
}
} //end inputHandler
void keyPressed() {
if(keyCode < keyDownArraySize) {
keyDown[keyCode] = true;
} //end if
} //this function also allows for pressing TWO+ keys at once
//so you can move and rotate at the same time!
void keyReleased() {
if(keyCode < keyDownArraySize) {
keyDown[keyCode] = false;
} //end if
}
1