2017-04-19 78 views
0

我已加載並且創建了兩個圖像按鈕menuStart和menuExit枝條menuStart.png和menuExit.png:處理 - mousePress圖像去起作用

圖像(menuStart,250,350,100,42);

image(menuExit,450,345,110,45);

我在做什麼: 我已將頁面設置爲階段。階段1是菜單,階段2是開始畫面階段3是選擇難度畫面+遊戲,階段4是退出畫面,階段5退出遊戲。我想使用mousePressed函數,這樣如果用戶在階段1中選擇menuStart按鈕,階段= 2。類似地,如果用戶在階段1中選擇menuExit按鈕,階段= 5。

我做了什麼與代碼: 我已經實現了mousePressed,但不知道如何設置mousePressed在圖像參數中的參數。我該如何着手設置?

代碼:

void doMenu() { 
    // Stage 1 Start -- MENU 
    if (stage == 1) { 
    textFont(title); 
    text("Game", 150, 200); 
    textFont(subtitle); 
    image(menuStart, 250, 350, 100, 42); 
    image(menuExit, 450, 345, 110, 45); 
    mousePressed(); 

    if(mousePressed == true) { 
     stage = 2; 
    } 
    } 
    // Stage 2 START 
    if (stage == 2) { 
    background(255); 
    startScreen = loadImage("start.png"); 
    image(startScreen, 0, 0, 800, 500); 

    if(mousePressed == true) { // true -->start 
     stage = 3; // go-to stage 3 
    } 
    /* else if(mousePressed == exit && stage != 2 { // exit 
     stage = 5; // go-to exit 
    } 
    */ 
    } 
    if(stage == 3) { 
    background(255); 
    startScreen = loadImage("start.png"); 
    image(startScreen, 0, 0, 800, 500); 
    text("Press N for Normal or H for Hard", 200, 375); 

    if(mousePressed == true) { // true --> hard 
     hard = true; 
     normal = false; 
     startMenu = false; 
    } 
    /* 
    else if(mousePressed == normal) { // normal 
     hard = false; 
     normal = true; 
     startMenu = false; 
    } 
    */ 
    /* 
    if(mousePressed == true) { // easy 
     hard = false; 
     normal = true; 
     startMenu = false; 
    } 
    */ 
    } 
    // Stage 4 EXIT 
    if (stage == 4) { 
    background(0); 
    exitScreen = loadImage("exit.jpg"); 
    image(exitScreen, 0, 0, 800, 400); 
    textFont(subtitle); 
    text("Press X to Exit", 300, 375); 
    if(mousePressed == true) { 
     stage = 5; 
    } 
    } 
    if(stage == 5) { 
    exit(); 
    } 
} 

回答

0

mousePressed變量不起作用您所描述的方式。它只是一個boolean變量,當按下鼠標時,它保存着true,當它不是時,它保存着false。它不包含任何被點擊的信息。

要檢查點擊的內容,您將不得不使用mouseXmouseY變量和if語句來檢查光標是否在按鈕內。一個簡單的例子是這樣的:

if(mouseX > buttonX && mouseX < buttonX + buttonWidth && mouseY > buttonY && mouseY < buttonY + buttonHeight){ 
    //mouse is inside button 
} 

如果按下鼠標以及鼠標按鈕內,則該按鈕被按下帶來。您可以使用嵌套if語句來檢查這一點,或者你可以使用mousePressed()功能:

void mousePressed(){ 
    if(mouseX > buttonX && mouseX < buttonX + buttonWidth && mouseY > buttonY && mouseY < buttonY + buttonHeight){ 
     //mouse is clicking button 
    } 
} 

無恥的自我推銷:here是做好利用輸入處理的教程。