首页 > 解决方案 > 处理语言中的翻转

问题描述

我正在做一个翻转动画。

如果当鼠标光标回到瓷砖时消失,我将如何将颜色恢复到原来的颜色?

在我的代码中,颜色褪为白色,然后当我将光标移回磁贴时不会回来

float addColorValue;
boolean clicked = false;

void setup() {
    size(400,400); //pixel size of the program
}
    
void draw() {
    background(255); //setting up the white background
    line(width/2,height,width/2,0); //drawing the vertical line
    line(width,height/2,0,width/2); //drawing the horizontal line
    if(clicked){
        if(mouseX > 200 && mouseY > 200){   // draws the 4th quadrant 
            fill(255,255,addColorValue+=1); rect(200,200,200,200); //draws rectangle
            fill(255); textSize(50); text("4TH", 250, 300); //display which number of the quadrant
        }
        //draws the 3rd quadrant
        else if(mouseX > 0 && mouseY > 200){ 
            fill(addColorValue+=1,addColorValue+=1,255); rect(0,200,200,200);
            fill(255); textSize(50); text("3RD", 50, 300);
        }
        //draws the 2nd quadrant
        else if(mouseX > 200 && mouseY > 0){ 
            fill(addColorValue+=1,255,addColorValue+=1); rect(200,0,200,200);
            fill(255); textSize(50); text("2ND", 250, 100);
        }
        //draws the 1st quadrant
        else if(mouseX > 0 && mouseY > 0){ 
            fill(255,addColorValue+=1,addColorValue+=1); rect(0,0,200,200);
            fill(255); textSize(50); text("1ST", 50, 100);
        }
    } 
    else{
        background(0);
    }
}
    
//switches on and off the lights of the program
void mousePressed() { 
    clicked = !clicked; 
}

标签: javaprocessing

解决方案


将每个四边形与索引相关联并将索引存储在全局变量 ( quad) 中。获取当前四边形 ( new_quad) 的索引并将其与quad. 如果索引已更改,则设置addColorValue = 0. 这将重新启动褪色效果:

if (quad != new_quad) {
    quad = new_quad;
    addColorValue = 0;
}

请参阅示例:

float addColorValue;
boolean clicked = false;
int quad = 0;

void setup() {
    size(400,400); //pixel size of the program
}

void draw() {
  
    if (clicked) {
        background(255); //setting up the white background
        line(width/2,height,width/2,0); //drawing the vertical line
        line(width,height/2,0,width/2); //drawing the horizontal line
   
        int new_quad = -1; 
        if (mouseX > 200 && mouseY > 200){   
            new_quad = 0;        
            drawRect("4TH", 200, 200, 200, 200, color(255, 255, addColorValue+=1)); 
        }
        else if(mouseX > 0 && mouseY > 200){ 
            new_quad = 1; 
            drawRect("3RD", 0, 200, 200, 200, color(addColorValue+=1, addColorValue+=1, 255)); 
        }
        else if(mouseX > 200 && mouseY > 0){ 
            new_quad = 2;   
            drawRect("2ND", 200, 0, 200, 200, color(addColorValue+=1, 255, addColorValue+=1)); 
        }
        else if(mouseX > 0 && mouseY > 0) {
            new_quad = 3;   
            drawRect("1ST", 0, 0, 200, 200, color(255, addColorValue+=1, addColorValue+=1)); 
        }
        
        if (quad != new_quad) {
            quad = new_quad;
            addColorValue = 0;
        }
    } 
    else{
        background(0);
    }
}

void drawRect(String text, int x, int y, int w, int h, color c) {
    fill(c); 
    rect(x, y, w, h);
    fill(255); 
    textSize(50); 
    text(text, x+50, y+100);
}

//switches on and off the lights of the program
void mousePressed(){ 
    clicked = !clicked; 
}

推荐阅读