I am having trouble with assigning a key press to a image and boolean variable

This is what I came up with on the spot.

void keyReleased() {
	if (keyPressed && ((key == 'a' || key == 'A')) {
			flagC1 =true
	}
}

void draw() { 
	if (keyPressed) {
		if (key == 'a' || key == 'A') {
			background(255,0,0);
		}
	}
	image(imgC1,350,350,300,50);
}

this is the rest of the code if you need it

PImage imgA,imgB,imgL; //images for beginning screens
PImage imgQ1,imgQ2,imgQ3,imgQ4; //images for the questions
PImage imgC1,imgC2,imgC3,imgC4; //answers for question 1

boolean flagA=true;
boolean flagB=true;
boolean flagL=false;
boolean flagQ1=false;
boolean flagC1=false;

void setup() {
size(1000,1000);
background(100,255,210); //background color
imgA = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-08%20at%209.48.03%20AM.png"); //img link
imgB = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-08%20at%209.44.31%20AM.png");
imgL = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-09%20at%2010.10.31%20AM.png");
imgQ1 = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-16%20at%208.50.12%20AM.png");
imgC1 = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-16%20at%209.50.02%20AM.png");
noStroke();
}

void draw() {
if (mousePressed && (mouseButton == LEFT)) {
background(225,0,0);
	noLoop();
}
	else if (mousePressed && (mouseButton == RIGHT)) {
background(0,225,0);
	noLoop();
	}
  
	if (flagA)
    image(imgA, 500, 530, 100, 50);
  if (flagB)
    image(imgB, 400, 400, 400, 50);
	if (flagL)
		image(imgL,400,430,200,150);
	if (flagQ1)
		image(imgQ1,300,300,400,80);
	if (flagC1)
		image(imgC1,350,350,300,50);
		
}

void mousePressed() {
  if (mouseButton == LEFT) {
		flagA=false;
		flagB=false;
		flagL=true;
		flagQ1=false;
  }

  if (mouseButton == RIGHT) {
    flagA=false;
		flagB=false;
		flagL=false;
		flagQ1=true;
  }
}

Thanks in advace for the help.

Are we sure you should use keyPressed inside of keyReleased??

I won’t use noLoop(); …

So switch keyReleased with keyPressed?

Try this:
void keyReleased() { if ( ((key == ‘a’

It did work, but it showed up before everything else. I want it to show up when “imgQ1” shows up.

Define ‚it‘ please

Not understand

“imgC1” or the image that I want to appear when “imgQ1” appears.

Image C1 shouldn’t appear

when
flagC1 is false

You never set flagC1 to true

Post your entire code

PImage imgA,imgB,imgL; //images for beginning screens
PImage imgQ1,imgQ2,imgQ3,imgQ4; //images for the questions
PImage imgC1,imgC2,imgC3,imgC4; //answers for question 1

boolean flagA=true;
boolean flagB=true;
boolean flagL=false;
boolean flagQ1=false;
boolean flagC1=false;

void setup() {
size(1000,1000);
background(100,255,210); //background color
imgA = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-08%20at%209.48.03%20AM.png"); //img link
imgB = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-08%20at%209.44.31%20AM.png");
imgL = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-09%20at%2010.10.31%20AM.png");
imgQ1 = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-16%20at%208.50.12%20AM.png");
imgC1 = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-16%20at%209.50.02%20AM.png");
noStroke();
}

void draw() {
if (mousePressed && (mouseButton == LEFT)) {
background(225,0,0);
}
	else if (mousePressed && (mouseButton == RIGHT)) {
background(0,225,0);
	}
  
	if (flagA)
    image(imgA, 500, 530, 100, 50);
  if (flagB)
    image(imgB, 400, 400, 400, 50);
	if (flagL)
		image(imgL,400,430,200,150);
	if (flagQ1)
		image(imgQ1,300,300,400,80);
	if (flagC1)
		image(imgC1,350,350,300,50);
		
}

void mousePressed() {
  if (mouseButton == LEFT) {
		flagA=false;
		flagB=false;
		flagL=true;
		flagQ1=false;
		flagC1=false;
  }

  if (mouseButton == RIGHT) {
    flagA=false;
		flagB=false;
		flagL=false;
		flagQ1=true;
		flagC1=true;
  }
}
/*
void keyReleased() {
	if ((key == 'a' || key == 'A')) {
			flagC1 =false
	}
}

void draw() { 
	if (keyPressed) {
		if (key == 'a' || key == 'A') {
			background(255,0,0);
		}
	}
	image(imgC1,350,350,300,50);
}
*/

The commented out code is the problem.

You can’t have 2 draw() – AND imgC1 is shown there always without the if bit?

this background is not permanent but just during you hold the mouse

To make it permanent, use a boolean variable.

you should in fact use a background throughout to see what’s going on. Without background images just stay on the screen from the previous execution / frame of draw().

Quiz

Anyway. When you want to do a Quiz, you need a counter for the question and a means of buttons to select the answer with the mouse, right? See Button / Examples / Processing.org

I deleted this one because of reasons of discretion

Thanks for the help.

1 Like