问题
I'm writing some code in Processing, and basically what I'm trying to do is get a blue square to "light up" (change to a lighter shade), and then back again. I wrote a superclass and a subclass, with a method in the subclass that's intended to do this lighting up business. Here's the code:
Superclass
abstract class Squares {
color darkBlue = color(0, 0, 204);
color brightBlue = color(0, 0, 255);
Squares(float x, float y) {
_x = x;
_y = y;
}
float _x;
float _y;
color _c;
abstract void drawSquare();
abstract void brighten();
abstract void darken();
}
Subclass
class blueSquare extends Squares {
blueSquare(float x, float y) {
super(x, y);
_c = darkBlue;
}
void drawSquare() {
fill(_c);
rect(_x, _y, 240, 240);
}
void brighten() {
_c = brightBlue;
this.drawSquare();
}
void darken() {
_c = darkBlue;
this.drawSquare();
}
void onOff() {
this.brighten();
delay(500);
this.darken();
}
String toString() {
return ("Blue color is" + _c);
}
}
Main Method
blueSquare blueSquare = new blueSquare(310, 310);
void setup() {
background(50);
size(600, 600);
drawSquares();
}
void draw() {
blueSquare.onOff();
println(blueSquare);
}
void drawSquares() {
strokeWeight(5);
blueSquare.drawSquare();
}
The onOff() method will only work at all (as in the color actually changing, as evidence by the println) in setup() and only if there is no delay() before it. How can I get this to work properly in draw() so that I can have it flash when I press a button, click the mouse, etc.? Thanks.
回答1:
Don't use delay inside draw, it won't work because draw will draw last color (it won't be called till everything is ended). I'll use a thread and a flag. The thread can wait that 500ms and then change the flag. Everytime you draw the rectangle you check that flag and change color filling. You can check thread in the reference.
Make this changes and it will work (leave Squares unchanged):
superclass:
blueSquare blue = new blueSquare(310, 310);
void setup() {
background(50);
size(600, 600);
}
void draw()
{
strokeWeight(5);
blue.drawSquare();
}
void mousePressed()
{
thread("onOff");
}
void onOff()
{
blue.flashing();
delay(500);
blue.noFlashing();
}
blueSquare
class blueSquare extends Squares
{
boolean _flashing;
blueSquare(float x, float y)
{
super(x, y);
this._c = darkBlue;
this._flashing=false;
}
void drawSquare()
{
if (this._flashing)
{
this._c=brightBlue;
}
else
{
this._c=darkBlue;
}
fill(_c);
rect(_x, _y, 240, 240);
}
void brighten()
{
this._c = brightBlue;
this.drawSquare();
}
void darken() {
_c = darkBlue;
this.drawSquare();
}
String toString()
{
return ("Blue color is" + _c);
}
void flashing()
{
this._flashing=true;
}
void noFlashing()
{
this._flashing=false;
}
}
回答2:
You shouldn't use the delay() function in an animated sketch. You should also not use separate threads.
Instead, use the millis() function or the frameCount variable to add timing to your animations.
Here is a simple example that shows a square that you can turn green for 1 second at a time:
int startFrame;
int duration = 60;
boolean on = false;
void draw() {
if (on) {
background(0, 255, 0);
} else {
background(0, 0, 255);
}
if (startFrame + duration < frameCount) {
on = false;
}
}
void mousePressed() {
startFrame = frameCount;
on = true;
}
Please note that this is just an example, but you can use these concepts to set up timing in your animations.
来源:https://stackoverflow.com/questions/44173837/class-method-not-working-properly-in-draw-in-processing