2016-10-03 48 views
0

矩形正在移动,当我点击它时,需要以0.1而不是3移动。我不知道如何对mousePressed部分进行编码,以便它始终保持0.1。如何让这个矩形变慢?

float stripeX = 0; 

void setup() { 

    size(500, 300); 
} 

void draw() { 
    background(255); 

    fill(10, 10, 100); 
    rect(stripeX, 90, 150, 250); 


    stripeX = stripeX + 3; 
    stripeX = stripeX % width; 
} 

void mousePressed() { 
    stripeX = stripeX - 2.9; 
} 

回答

-1

这一切都有点冒险。 draw()被多久调用一次?在每一帧?一般来说,调整draw函数中的东西是一个糟糕的主意,它应该画出来。

要破解它有点

float stripeX = 0; 
float deltaX = 3.0; 

void draw() 
{ 
    //omitted some code 
    stripeX += deltaX; 
} 

void mousePressed() 
{ 
    if(deltaX > 0.1) 
     deltaX = 0.1; 
    else 
     deltaX = 3.0; // let a second press put it back to 3.0 
} 

但是你可能想要把它回3.0鼠标了。如果您有足够的信息知道如何拦截该事件,您还没有 。

0

你可以只使用mousePressed变量随着draw()函数内的if声明:

float stripeX = 0; 

void setup() { 
    size(500, 300); 
} 

void draw() { 
    background(255); 

    fill(10, 10, 100); 
    rect(stripeX, 90, 150, 250); 

    if(mousePressed){ 
    stripeX = stripeX + .1; 
    } 
    else{ 
    stripeX = stripeX + 3; 
    } 

    stripeX = stripeX % width; 
} 
0

在你的情况下,最好的办法是使用的mouseReleased()方法:

float stripeX, deltaX; 

void setup() { 
    size(500, 300); 
    stripeX = 0f; // init values here, in setup() 
    deltaX = 3f; 
} 

void draw() { 
    background(255); 
    fill(10, 10, 100); 
    rect(stripeX, 90, 150, 250); 
    stripeX += deltaX; 
    stripeX = stripeX % width; 
} 

void mousePressed(){ 
    deltaX = 0.1; 
} 

void mouseReleased(){ 
    deltaX = 3f; 
}