when either w, a, s or d is pressed my code moves my image and cycles through different ones. i want it to show image "midas[1]’ if none of these buttons is pressed. i have a line of code
if (key != 'w' && key!= 'a' && key!='s' && key != 'd') {
age(midas[1], midasx, midasy, 50, 80);
}
that i thought would do this but it only does this at the beginning and not when i stop pressing a button.
here’s the rest of my code
PImage[] midas= new PImage[8];
float midasx= 1;
float midasy= 1;
int i=1;
void setup() {
size(600, 600);
midas[1] = loadImage("1.png");
midas[2] = loadImage("2.png");
midas[3] = loadImage("3.png");
midas[4] = loadImage("4.png");
midas[5] = loadImage("5.png");
midas[6] = loadImage("6.png");
midas[7] = loadImage("7.png");
}
void draw() {
background(0, 0, 0);
tint(255, 255);
if (key != 'w' && key!= 'a' && key!='s' && key != 'd') {
image(midas[1], midasx, midasy, 50, 80);
}
if (keyPressed) {
if (key == 'w') {
midasy=midasy-10;
image(midas[i], midasx, midasy, 50, 80);
}
}
if (keyPressed) {
if (key == 's') {
midasy=midasy+10;
image(midas[i], midasx, midasy, 50, 80);
}
}
if (keyPressed) {
if (key == 'a') {
midasx=midasx-10;
image(midas[i], midasx, midasy, 50, 80);
}
}
if (keyPressed) {
if (key == 'd') {
midasx=midasx + 10;
image(midas[i], midasx, midasy, 50, 80);
}
}
i++;
delay(100);
if (i==7) {
i=1;}}
1 Like
Chrisir
September 9, 2021, 7:56pm
2
you can set variable boolean showMidas1 = true; before setup
In keyPressed Wasd set it to false
make a new function
void keyReleased() {
showMidas1 = true;
}
1 Like
the image shows up twice now
PImage[] midas= new PImage[8];
boolean showMidas1= true;
float midasx= 1;
float midasy= 1;
int i=1;
void setup() {
size(600, 600);
midas[1] = loadImage("1.png");
midas[2] = loadImage("2.png");
midas[3] = loadImage("3.png");
midas[4] = loadImage("4.png");
midas[5] = loadImage("5.png");
midas[6] = loadImage("6.png");
midas[7] = loadImage("7.png");
}
void draw() {
background(0, 0, 0);
tint(255, 255);
if(showMidas1=true){
image(midas[1], midasx, midasy, 50, 80);}
if (keyPressed) {
if (key == 'w') {
midasy=midasy-10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 's') {
midasy=midasy+10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 'a') {
midasx=midasx-10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 'd') {
midasx=midasx + 10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
i++;
delay(100);
if (i==7) {
i=1;}}
Chrisir
September 9, 2021, 8:07pm
4
MoonAlien822:
if(showMidas1=true){
Please == double ==
and also place else {
after this line
Chrisir
September 9, 2021, 8:08pm
5
And } after the entire keyPressed section
Remember to use ctrl-t in processing to get automatically format
it has now stopped doing anything
PImage[] midas= new PImage[8];
boolean showMidas1= true;
float midasx= 1;
float midasy= 1;
int i=1;
void setup() {
size(600, 600);
midas[1] = loadImage("1.png");
midas[2] = loadImage("2.png");
midas[3] = loadImage("3.png");
midas[4] = loadImage("4.png");
midas[5] = loadImage("5.png");
midas[6] = loadImage("6.png");
midas[7] = loadImage("7.png");
}
void draw() {
background(0, 0, 0);
tint(255, 255);
if (showMidas1==true) {
image(midas[1], midasx, midasy, 50, 80);
} else {
if (keyPressed) {
if (key == 'w') {
midasy=midasy-10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 's') {
midasy=midasy+10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 'a') {
midasx=midasx-10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 'd') {
midasx=midasx + 10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
}
i++;
delay(100);
if (i==7) {
i=1;
}
}
still not working
PImage[] midas= new PImage[8];
boolean showMidas1= true;
float midasx= 1;
float midasy= 1;
int i=1;
void setup() {
size(600, 600);
midas[1] = loadImage("1.png");
midas[2] = loadImage("2.png");
midas[3] = loadImage("3.png");
midas[4] = loadImage("4.png");
midas[5] = loadImage("5.png");
midas[6] = loadImage("6.png");
midas[7] = loadImage("7.png");
}
void draw() {
background(0, 0, 0);
tint(255, 255);
if (showMidas1==true) {
image(midas[1], midasx, midasy, 50, 80);
}
if (keyPressed) {
if (key == 'w') {
midasy=midasy-10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 's') {
midasy=midasy+10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 'a') {
midasx=midasx-10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 'd') {
midasx=midasx + 10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
i++;
delay(100);
if (i==7) {
i=1;
}}
void keyReleased() {
showMidas1 = true;
}
it still shows two images at the same time sometimes
Chrisir
September 9, 2021, 8:29pm
15
Actually you can move the Display of the image out of the if-clauses so there is only one line at the end
Use the displaying with an if (showMidas1==false){…
hey back with another question, i added another object “daughter” i would like “daughter” to change to a different image when “midas” touches it
PImage[] midas= new PImage[8];
PImage[] daughter= new PImage[3];
boolean showMidas1= true;
float midasx= 1;
float midasy= 1;
float daughterx= width;
float daughtery= height;
int i=1;
int i2= 1;
void setup() {
size(600, 600);
midas[1] = loadImage("1.png");
midas[2] = loadImage("2.png");
midas[3] = loadImage("3.png");
midas[4] = loadImage("4.png");
midas[5] = loadImage("5.png");
midas[6] = loadImage("6.png");
midas[7] = loadImage("7.png");
daughter[1] = loadImage("daughter.png");
daughter[2] = loadImage("daughter gold.png");
}
void draw() {
background(0, 0, 0);
tint(255, 255);
if(midas and daughter are touching){
i2=2;}
image(daughter[i2], daughterx, daughtery, 40, 70);
if (showMidas1==true&& ! keyPressed) {
image(midas[1], midasx, midasy, 50, 80);
}
if (keyPressed) {
if (key == 'w') {
midasy=midasy-10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 's') {
midasy=midasy+10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 'a') {
midasx=midasx-10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
if (keyPressed) {
if (key == 'd') {
midasx=midasx + 10;
image(midas[i], midasx, midasy, 50, 80);
showMidas1= false;
}
}
i++;
delay(100);
if (i==7) {
i=1;
}}
void keyReleased() {
showMidas1 = true;
}
Chrisir
September 11, 2021, 5:20pm
17
first allow me to add a comment to this:
arrays start with 0, so first entry should be midas[0] = loadImage(“1.png”);
Your question
Yeah, use dist() command here - see reference
something like
if(dist(midasX,midasy, daughterx,daughtery) < 45) {
please note that this works best, when you use the center of both images (and not upper left corner for example; so maybe say midasx+midaswidth/2
) (and take into account the sum of their radii to know what value is best where I said 45)
(There are more advanced formulas for rect/rect collision but that should be enough)
Chrisir
1 Like