2016-10-03 52 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; 
}