We are about to switch to a new forum software. Until then we have removed the registration on this forum.
In stageCheck() I have a else statement. My question is, why does the background change only when I press down on the mouse. I would like to set the color when I click the mouse and it stays that color until I click the mouse again. What am I doing wrong?
Ball redhall;
Ball bluehall;
Ball greenhall;
Stage colorStage;
boolean day = true;
class Ball {
color ballcolor; // color
float r; // radius
float ballX; //location
float ballY; //location
float dx; //delta x speed
float dy; //delta y speed
// Constructor
Ball(color tempballColor, float tempBallX, float tempBallY ) {
ballcolor = tempballColor;
r = 15;
ballX = tempBallX;
ballY = tempBallY;
dx = random( - 5,5);
dy = random( - 5,5);
}
void move() {
ballX += dx; // Increment x
ballY += dy; // Increment y
// Check horizontal edges
if (ballX > width-60 || ballX < 60) {
dx *= random( - 1, -5);
}
//Check vertical edges
if (ballY > height-60 || ballY < 60) {
dy *= random( - 1, -3);
}
}
// Draw the ball
void display() {
stroke(0);
fill(ballcolor);
ellipse(ballX,ballY,r*2,r*2);
}
}
class Stage {
color stagecolor; // color
float stageX; //location
float stageY; //location
float stageW; //height
float stageH; //width
// Constructor
Stage(color tempstageColor) {
stagecolor = tempstageColor;
stageX=50; //location
stageY=50; //location
stageW=500; //width
stageH=400; //height
}
// Draw the stage
void display() {
noStroke();
fill(stagecolor);
rect(stageX,stageY,stageW,stageH);
}
void stageCheck(){
if (mouseX > stageX && mouseX < stageX+stageW && mouseY > stageY && mouseY < stageY+stageH && mousePressed) {
day = false;
nightBackground();
}else{
day = true;
dayBackground();
}
}
}
void setup() {
size(600,500);
smooth();
colorStage = new Stage(color(184,241,250));
// Initialize balls
redhall = new Ball(color(255,0,0),70,70);
bluehall = new Ball(color(5,184,82),70,70);
greenhall = new Ball(color(16,155,252),70,70);
}
void draw() {
background(145,189,232);
screen();
}
void screen(){
colorStage.stageCheck();
colorStage.display();
// Move and display balls
redhall.move();
redhall.display();
bluehall.move();
bluehall.display();
greenhall.move();
greenhall.display();
}
void red() {
redhall = new Ball(color(255,0,0),70,70);
}
void blue() {
bluehall = new Ball(color(5,184,82),70,70);
}
void green() {
greenhall = new Ball(color(16,155,252),70,70);
}
void nightBackground() {
colorStage = new Stage(color(188,188,188));
}
void dayBackground() {
colorStage = new Stage(color(184,241,250));
}
void keyPressed() {
if (key == '1') red();
if (key == '2') blue();
if (key == '3') green();
}
Answers
Because mousePressed is only ever true when the mouse button is currently pressed.
Perhaps you want to keep track of another boolean variable that stores whether the mouse was ever pressed?
In screen() say if day daybackground else nightbackground
The other works only when mouse is pressed, remmember?
Use the value of var day
Tried out your code got carried away and came up with this.
CODE REMOVED by me
Hooray full code solutions... /s
@KevinWorkman
Actually nearly all the code I posted belonged to the OP, I simply reworked the part of it concerning the stage colour. But I take your point so have removed the lot.
It's up to you whether you want to provide full code solutions.
I just believe that providing smaller hints is more helpful, and I respectfully point that out when I see people doing OP's work for them.