ArrayIndexoutofbounds with image objects
in
Programming Questions
•
1 year ago
I got array out of bounds issues.
when i try to put images as objects in my code
( simply rect primitives in the ancient code );
i have
ERROR MESSAGE Arrayoutofboundsexception coordinate out of bounds
when i try the function display() without img.resize the code work.
so it's only about img.resize
i tried img.resize(xpos,ypos); and also img.resize(frameCount,frameCount) but the result is the same
could you explain me more about this array index out of bounds?
is it about " if (xpos > width) {
xpos = 0; ? "
best regards
-----------------------
Car myCar1;
Car myCar2;
ArrayList myCar;
PImage img;
void setup() {noStroke();
size(700,400);
img=loadImage("144.jpg");
myCar = new ArrayList();
}
void draw() {
background(255);
for(int i = myCar.size()-1;i>=0;i--){
Car ball = (Car) myCar.get(i);
ball.move();
ball.display();
if(frameCount>160){frameCount=ceil(cos(frameCount));}
}
}
void mouseMoved() {
myCar.add(new Car(color(frameCount), mouseX, mouseY,random(30),1));
}
class Car {
color c;
float xpos;
float ypos;
float xspeed;float gravity;float life = 255;float xc=2;
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed,float tempXc) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed; gravity = 0.1;xc = tempXc;
}
void display() {img.resize(int(xpos),int(ypos));
// strokeWeight(frameCount/80);noStroke();
// fill(frameCount,mouseY, mouseX);
// rectMode(CENTER);
// rect(xpos,ypos,100-frameCount,80-frameCount);
image(img,xpos,ypos);>>>>>>>>>ERROR MESSAGE Arrayoutofboundsexception coordinate out of bounds
}
void drive() {
xpos = xpos + xspeed;
xspeed = xspeed + xc;
if (xpos > width) {
xpos = 0;
}
}
void move() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
}
void mousePressed(){frameCount=0;}
void mouseDragged(){}
1