Random image array question
in
Programming Questions
•
9 months ago
i have a sketch with images bouncing around the screen. In the original sketch it was balls(ellipse) bouncing around, thats why my sketch have a numBalls variable, and not a numImages variable(i was too lazy to rename),
i can create as many images as i want.
If i create 10 images bouncing around, they will all have the same image, what i want is for them to have a randomly picked image. so that maybe 2 of them have a picture of a basketball, and 3 a golfball and so forth.
i can create as many images as i want.
If i create 10 images bouncing around, they will all have the same image, what i want is for them to have a randomly picked image. so that maybe 2 of them have a picture of a basketball, and 3 a golfball and so forth.
In my current code i am trying to make the array pick any of the 5 images i have in the array. What it does is it loops all the images from the array when i hit play. I know it is because i have it in the draw function, so it updates the imagearray all the time. How can i get the array to pick one image, hold on to that image and then choose another one?
The array is in the void display() function. Also this is not the full code, but the rest of it doesn't matter i think.
Thanks for any help :)
Thanks for any help :)
- import cosm.*;
- int numBalls = 100; // Max number of images
- float spring = 0.05;
- float gravity = 0.03;
- float friction = -0.9;
- PImage images;
- Ball[] balls = new Ball[numBalls];
- PImage[] myImageArray = new PImage[5];
- void setup() {
- size(800, 500);
- noStroke();
- for (int i = 0; i < numBalls; i++) {
- balls[i] = new Ball(random(width), 20, random(30, 70), i, balls);
- }
- for (int i=0; i<myImageArray.length; i++) {
- myImageArray[i] = loadImage( "sopa" + i + ".jpg");
- }
- }
- void draw() {
- }
- background(0);
- for (int i = 0; i < numBalls; i++) {
- balls[i].collide();
- balls[i].move();
- balls[i].display();
- }
- void display() {
- fill(255, 204);
- image(myImageArray[(int)random(5)], x, y, diameter, diameter); // Changing the images randomly (not working as it should)
- }
- }
1