Hey!
First of all, I see you’re creating an array of images with 10 spaces right off the bat and another array with 2 images.
Be aware that this doesn’t actually load any images into the array, which is a collection of variables. running new PImage[10]
simply allows the memory allocation of 10 null PImage objects.
Second, you did a good job creating your setup! the first two lines are good.
I can see you then generate an int between 0 and 9. Good job on that too. Be aware though that flattening the number using int will mean that you have an extremely low chance of getting a 9. You’ll be generating a number between 0 and 9 but then cutting off the decimal. A good way to fix this might be to change the line to rand=int(random(0,9.9)) so it can include numbers up to 10 (but not including)
I then see that you generate a string with a typical format of something like “head_004.jpg” for example. You don’t explain what files you have and where they’re stored so I can’t help you with that (you may want to consider including something like that in your problem next time). If that’s an example for what the file name looks like but you also might want to verify the path as well. More on that in a moment.
Enter the takerandomimage function…
The first line is off to a bit of a bad start. loadImage() takes in a string but returns a single image. You’re setting the value of head to a single image but since head is an array, you’ll have a type incompatibility. You can’t put an image into a box full of empty spaces, you have to put an image into a box expecting a single image. Changing the first line of the program to simply PImage head;
will fix this issue.
Still on this line however, you’re taking the filename earlier and putting it into this function. To ensure your file will open you will want to make sure you are referencing its path properly. An easy way to do this is to save your processing project somewhere, make a folder in the same directory as your main .pde files called ‘data’ and place your images in there. Then, make sure that whenever you’re running loadImage() to enter a string that starts with data/
if you have a file called head_005.png inside of your data folder and you run that line after making the other change above, this line should work.
I recommend reading the first few lines in the description of the loadImage() documentation page if you’d like to learn more.
The next two lines in takeRandomimage should work after making those changes.
mousePressed(redraw) will do nothing since you don’t have a draw function and you haven’t called noLoop. I recommend either
a. making a draw function and putting your takerandomimage and rand= lines in there and running noDraw() and redraw() in setup(), and then changing the final lines of your program to
void mouseClicked(){
redraw();
}
or
b. putting your takerandomimage and rand lines and putting them in a new function and calling them from a mouseclicked function such as
void newFace(){
background(0);
the rand= line
the takerandomimage line
}
void mouseClicked(){
newFace();
}
I see you stringing together a bunch of concepts that can easily be found from guides on the internet but there’s not a lot of comprehension or reading the publicly available documentation. I recommend you try watching a few processing instructional videos on the coding train or checking out some of processing’s really good documentation.
Good luck!