2015-10-03 24 views
0

我想製作一個程序,在點擊「Roll」的矩形後單擊顯示骰子的面部,但是當我點擊時,沒有任何反應。試圖在Processing中創建一個骰子,但點擊不會觸發它

有人可以解釋我做錯了什麼嗎?

import java.util.Random; 

public Random random = new Random(); 

public color purple = #B507F5; 
public int diceChoose = random.nextInt(6); 
public int x = mouseX; 
public int y = mouseY; 


public void setup() { 
    size(750, 900); 
    background(255); 

} 

public void draw() { 
    strokeWeight(3); 

    //dice roll button 
    rect(100, 700, 550, 150); 
    textSize(100); 
    fill(purple); 
    text("Roll", 280, 815); 
    noFill(); 

    //dice face 
    rect(100, 100, 550, 550); 

    roll(); 
} 

public void one() { 
    fill(0); 
    ellipse(375, 375, 100, 100); 
    noFill(); 
} 

public void two() { 
    fill(0); 
    ellipse(525, 225, 100, 100); 
    ellipse(225, 525, 100, 100); 
    noFill(); 
} 

public void three() { 
    fill(0); 
    ellipse(375, 375, 100, 100); 
    ellipse(525, 225, 100, 100); 
    ellipse(225, 525, 100, 100); 
    noFill(); 
} 

public void four() { 
    fill(0); 
    ellipse(525, 225, 100, 100); 
    ellipse(225, 525, 100, 100); 
    ellipse(525, 525, 100, 100); 
    ellipse(225, 225, 100, 100); 
    noFill(); 
} 

public void five() { 
    fill(0); 
    ellipse(375, 375, 100, 100); 
    ellipse(525, 225, 100, 100); 
    ellipse(225, 525, 100, 100); 
    ellipse(525, 525, 100, 100); 
    ellipse(225, 225, 100, 100); 
    noFill(); 
} 

public void six() { 
    fill(0); 
    ellipse(525, 225, 100, 100); 
    ellipse(225, 525, 100, 100); 
    ellipse(525, 525, 100, 100); 
    ellipse(225, 225, 100, 100); 
    ellipse(525, 375, 100, 100); 
    ellipse(225, 375, 100, 100); 
    noFill(); 
} 

public void roll() { 
    if (mousePressed && x > 100 && x < 650 && y > 700 && y < 850) { 
    diceChoose = random.nextInt(6); 
    if (diceChoose == 0) { 
     one(); 
    } 
    else if (diceChoose == 1) { 
     two(); 
    } 
    else if (diceChoose == 2) { 
     three(); 
    } 
    else if (diceChoose == 3) { 
     four(); 
    } 
    else if (diceChoose == 4) { 
     five(); 
    } 
    else if (diceChoose == 5) { 
     six(); 
    } 
    } 
} 
+0

你是什麼意思,它有什麼有點不對? –

+0

我沒有意識到我可以在發佈評論前編輯帖子。 –

回答

0

您設定xy變量等於mouseXmouseY在節目的一開始。但那不是意味着當mouseXmouseY更改時,您的xy變量也將更改。試想一下:

float x = 100; 
float y = x; 
x = 200; 
println(y); //prints 100! 

因此,而不是指xy(這永遠不會改變),你需要的,如果語句中使用mouseXmouseY在此:

if (mouseX > 100 && mouseX < 650 && mouseY > 700 && mouseY < 850) { 

那麼你有其他問題(你實際上沒有發現點擊),但這是你第一個問題的答案。

順便說一下,我想通過簡單地添加println()語句的方式。我把一個你的if語句之前和它的一個內:

println("here 1"); 
    if (x > 100 && x < 650 && y > 700 && y < 850) { 
    println("here 2"); 

的「這裏1」打印出來,但「這裏2」沒有,所以我知道看的邏輯更接近你的if語句。將來,您可以嘗試自己進行這種類型的調試,爲自己節省下一篇文章的麻煩!

相關問題