How can I alternate multiple images being on top with keyPressed?

edited March 2018 in How To...

I'm creating a mock computer screen where when you press certain letters, an image will show on screen. I want it so the letters can be pressed at random and not in a set order. So if I press the 's' key an image will show, if I press 'r' another image will show on top of 's' image. But if I press 's' again, I want the 's' image to be on top again. How can I accomplish this?

Tagged:

Answers

  • maybe something like this

    photoS = loadImage("fotoS.jpg");
    photoA= loadImage("fotoA.jpg");
    photoD = loadImage("fotoD.jpg");
    photoX = loadImage("fotoX.jpg");
    
    void draw(){
    switchImage();
    }
    
    void switchImage(){
    if (keyPressed) {
        if (key == 's' || key == 'S') {
          image(photoS, xPos, yPos);
    }
        if (key == 'a' || key == 'A') {
          image(photoA, xPos, yPos);
    }
        if (key == 'd' || key == 'D') {
          image(photoD, xPos, yPos);
    }
        if (key == 'x' || key == 'X') {
          image(photoX, xPos, yPos);
    }
    and so on...
    }
    
  • Perfect! Thanks so much for the help.

  • you're welcome ;)

Sign In or Register to comment.