I am running into an issue when I try to set a texture to a PShape from an array in my project, and then increment the index to the array.
This is my Array reference mndImg[imgChsr]
I have a key press that just basically says imgChsr ++; It modulos and wraps just fine.
It works when I am just drawing an image. I can step through all my images no problem.
image(mndImg[imgChsr], 0, 0, mndImg[imgChsr].width*, mndImg[imgChsr].height*imgScl);
but when I try to set a texture like this.
plane.setTexture(mndImg[imgChsr]);
It works when my object is created but then when I hit my key to increment imgChsr it seems to slam the value to -2, i get the error ArrayIndexOutOfBoundsException: -2
As I said, I know the imgChsr works because when just doing it with an image, I can go through all the images no problem. It seems to be specific to incrementing the index in the setTexture call.
Thanks in advance for any insight!
Toof
Hello, I have boiled this down further to the following code.
What I have pasted here will crash as is.
PShape plane;
/* Global Image Array ///////////////////////////*/
int numImgs = 3;
PImage[] mndImg = new PImage[numImgs];
int imageChooser = 0;
//SETUP///////////////////////////////////////////////////////
void setup() {
size(800, 800, P3D);
noStroke();
//Image Loads -- IMAGE ARRAY Fill-up////////////////////////
mndImg[0] = loadImage("Outer_Large_Fan.png");
mndImg[1] = loadImage("Mid_Medium_1.png");
mndImg[2] = loadImage("Mid_Medium_2.png");
plane = createShape(RECT, 0, 0, 100, 100);
}//END SETUP//////////////////////////////////////////////////////
//DRAW////////////////////////////////////////////////////////////
void draw() {
background(200);
shapeMode(CENTER);
translate(width/2,height/2);
plane.setTexture(mndImg[imageChooser]);
shape(plane,0,0);
//image(mndImg[imageChooser], 0, 0);
println("ImageChooser = ",imageChooser);
}//END DRAW///////////////////////////////////////////////////////
//KEY PRESSES///////////////////////////////////////////////////
void keyPressed () {
if (keyPressed) {
// Choose Texture for Plane Object /////////////////////
if (key == 'k') {
imageChooser ++;
imageChooser = imageChooser % numImgs;
println("image chooser is now ", imageChooser);
}
}
}```
IF I comment the plane out, and comment the image call in it works.
``` //plane.setTexture(mndImg[imageChooser]);
//shape(plane,0,0);
image(mndImg[imageChooser], 0, 0);
If I go back to using the plane AND comment out the noStroke(); in setup it works!
...
//noStroke();
...
plane.setTexture(mndImg[imageChooser]);
shape(plane,0,0);
//image(mndImg[imageChooser], 0, 0);
I really don’t want these stroked and I tried putting noStroke(); in draw instead and it has no effect. I get a stroked plane…
Any thoughts? Is this just a bug? Is there a fix I could use?
thank you!
Toof
Also I tried using
texure(mndImg[imageChooser]);
but that only works between
beginShape(); endShape();
also tried adding
plane.disableStyle();
not working, guess I will just try hand building the rect by hand instead.