multiple background images
in
Programming Questions
•
10 months ago
I need to implement 5 different background images into my code
These 5 images needs to be paired with sets of 2 images that slides to the side when a switch is pushed.
When they slide to the side the background image is shown, but the problem is that it shows the same background image each time.
When pushing another switch a new set of images slides in, and when these slide out of the frame again a new background should be visible Instead.
This system functions with 5 pairs of 2 images, and each pair should have a corresponding background image. I really hope someone can help me
This is my code now:
- PImage[] img;
- PImage bg;
- boolean doorContactOpening = false; // variables for the door sensors.
- boolean doorContactClosing = false;
- int openDoor = 0; // variable for the degree of which the door is open
- int doorSpeed = 3; // Setting the speed of the doors
- long lastUpdate;
- int currentImage = 1;
- int a;
- void setup() {
- size(640, 360);
- frameRate(30);
- bg = loadImage("moonwalk.jpg");
- img = new PImage[10];
- img[0] = loadImage("moonwalk1a.jpg"); // Load the images into the program
- img[1] = loadImage("moonwalk1b.jpg");
- img[2] = loadImage("moonwalk2a.jpg");
- img[3] = loadImage("moonwalk2b.jpg");
- img[4] = loadImage("moonwalk3a.jpg");
- img[5] = loadImage("moonwalk3b.jpg");
- img[6] = loadImage("moonwalk4a.jpg");
- img[7] = loadImage("moonwalk4b.jpg");
- img[8] = loadImage("moonwalk5a.jpg");
- img[9] = loadImage("moonwalk5b.jpg");
- lastUpdate = millis();
- }
- void draw() {
- if (openDoor >= (width/2)){
- currentImage = int(random(0,5));
- }
- background(bg);
- a = (a + 1)%(width+32);
- stroke(640, 460, 0);
- line(0, a, width, a-26);
- line(0, a-6, width, a-32);
- if (millis()-lastUpdate > 1){
- if (doorContactOpening == true){
- if(openDoor<(width/2)){
- openDoor += doorSpeed;
- }
- image(img[currentImage*2],-openDoor, 0);
- image(img[currentImage*2+1],(width/2)+openDoor, 0);
- } else if (doorContactClosing == true){
- if(openDoor>0){
- openDoor -= doorSpeed;
- }
- image(img[currentImage*2],-openDoor, 0);
- image(img[currentImage*2+1],(width/2)+openDoor, 0);
- } else {
- image(img[currentImage*2],-openDoor, 0);
- image(img[currentImage*2+1],(width/2)+openDoor, 0);
- }
- lastUpdate = millis();
- } else {
- image(img[currentImage*2],-openDoor, 0);
- image(img[currentImage*2+1],(width/2)+openDoor, 0);
- }
- }
- void keyPressed()
- {
- if( key == 'o') {
- doorContactClosing = false;
- doorContactOpening = true;
- } else if( key == 'c') {
- doorContactOpening = false;
- doorContactClosing = true;
- }
- }
1