2016-07-18 19 views
2

Ich machte ein Whack a Mole Stil Spiel und ich benutze die mousePressed() Methode, um die Maulwürfe zu schlagen. Der Code funktioniert gut, nur das Problem besteht darin, dass es sich dabei um einen Spiel-brechenden Exploit handelt, den Sie einfach mit der Maus ausführen können. Im Grunde genommen, anstatt jedes Mal mit der Maus zu klicken, können Sie sie einfach gedrückt halten, um jeden Maulwurf zu erhalten. Um dies zu beheben, habe ich stattdessen mouseClicked() verwendet - dasselbe Problem. Dies ist möglicherweise auf den booleschen Wert zurückzuführen, den ich in mousePressed() habe, aber ich weiß es nicht, da ich ein ähnliches Problem in einem anderen Spiel habe, das ich codiere, wo es keine boolesche Variable gibt. Ich möchte, dass das Spiel dazu führt, dass man jedes Mal klicken muss, wenn man einen Maulwurf schlagen will, irgendwelche Lösungen? Ich dachte über die Verwendung eines Timers anstatt der Fehlersuche mit für wie 3 Stunden, die ich gerade hier kam. Danke - hier ist der Code:Verarbeitung: mousePressed()/mouseClicked() Zecken zu oft

package whackmole; 

import processing.core.PApplet; 
import processing.core.PFont; 
import processing.core.PImage; 
import java.util.Random; 


public class WHACKMOLE extends PApplet { 

PImage mole; 
PImage mallet1; 
PImage mallet2; 

PFont f; 
public int timer; 
public int startTime; 
public int gameTime; 
public int startGameTime; 


int score = 0; 
Random rnd = new Random(); 
boolean mouseP = false; 
int life = 3; 


Mole mole1; 
Mole mole2; 
Mole mole3; 
Mallet mallet; 
enum GameState { 
     MENU, 
     RUNNING, 
     RUNNING2 
    } 
static GameState currentState; 

public void setup() { 
    size(1000, 800); 

    currentState = GameState.MENU; 
    mole = loadImage("mole.png"); 
    mole1 = new Mole(mole); 
    mole2 = new Mole(mole); 
    mole3 = new Mole(mole); 
    f = createFont("comic.tff",16,true); 
    textFont(f,36); 
} 

public void draw() { 

     switch(currentState){ 
     case MENU: 
      drawMenu(); 
      startTime = millis(); 
      timer = 0; 
      life = 3; 
      gameTime = millis(); 
      cursor(CROSS); 
      score = 0; 
      break; 
     case RUNNING: 
      drawRunning(); 
      break; 
     case RUNNING2: 
      drawRunning2(); 
      gameTime = millis() - startGameTime; 

      break; 

     } 


    } 

public void drawRunning() { 
    clear(); 

    background(50,255,50); 

    if(timer < 60000){ 
    mallet2 = loadImage("mallet2.png"); 
    timer = millis(); 



    mole1.drawMole(); 
    mole1.collision(mallet); 
    timer = millis() - startTime; 
    mallet1 = loadImage("mallet1.png"); 
    mallet = new Mallet(mallet1, mouseX, mouseY); 

    fill(255,255,255); 
    text("Time: " + ((60 - timer/1000)), 850, 50); 

    if (mouseP){ 
     mallet.drawMallet(mallet2, mouseX, mouseY); 
    } 
    else { 
     mallet.drawMallet(mallet1, mouseX, mouseY); 
    } 
    if(timer > 60000){ 
     fill(255,255,255); 
     text("Game over!" , 400, 400); 
     try { 
      Thread.sleep(4000); 
     } catch (InterruptedException e) { 

     } 
     currentState = GameState.MENU; 

    } 
    noCursor(); 
    text("Score: " + score ,25,50); 
    } 

} 

public void drawRunning2() { 
clear(); 
mallet1 = loadImage("mallet1.png"); 
mallet = new Mallet(mallet1, mouseX, mouseY); 


mallet2 = loadImage("mallet2.png"); 


background(50,255,50); 

timer = millis() - startTime; 

text("Life: " + life ,25,50); 

noCursor(); 

text("Time: " + (gameTime/1000), 825, 50); 
if(life <= 0){ 
    mole1.dead = true; 
    mole2.dead = true; 
    mole3.dead = true; 
    text("Game over!" , 400, 400); 
    try { 
     Thread.sleep(4000); 
    } catch (InterruptedException e) { 

    } 


    currentState = GameState.MENU; 
     timer = 0; 
     gameTime = 0; 
     startGameTime = millis(); 



} 

if (timer < 1800){ 
    if (!mole1.dead){ 
     mole1.drawMole(); 
     mole1.collision(mallet); 
    } 
    if (!mole3.dead){ 
     mole3.drawMole(); 
     mole3.collision(mallet); 
    } 
    if (!mole2.dead){ 
     mole2.drawMole(); 
     mole2.collision(mallet); 
    } 
    if (mouseP){ 
     mallet.drawMallet(mallet2, mouseX, mouseY); 
    } 
    else { 
     mallet.drawMallet(mallet1, mouseX, mouseY); 
    } 
} 
else { 
    startTime = millis(); 
    if (!mole1.dead || !mole2.dead || !mole3.dead) { 
     life --; 
    } 
    if (life > 0){ 
     mole1.dead = false; 
     mole2.dead = false; 
     mole3.dead = false; 

     mole1.xPos = rnd.nextInt(950); 
     mole1.yPos = rnd.nextInt(600); 
     mole3.xPos = rnd.nextInt(950); 
     mole3.yPos = rnd.nextInt(600); 
     mole2.xPos = rnd.nextInt(950); 
     mole2.yPos = rnd.nextInt(600); 



     } 




    } 
} 





public void drawMenu(){ 
    clear(); 
    background(142,22,178); 

    fill(165, 119, 249); 
    rect(250, 150, 500, 200); 
    fill(255,255,255); 
    text("Time Mode", 375, 270); 
    fill(165, 119, 249); 
    rect(250, 450, 500, 200); 
    fill(255,255,255); 
    text("Survival Mode", 375, 570); 


} 

     public void mousePressed() 
     { 
      mouseP = true; 

      if(currentState == GameState.MENU && mouseX > 250 && mouseX < 750 && mouseY > 150 && mouseY < 350){ 
       currentState = GameState.RUNNING; 
      } 
      if(currentState == GameState.MENU && mouseX > 250 && mouseX < 750 && mouseY > 450 && mouseY < 650){ 
       currentState = GameState.RUNNING2; 
     } 

     } 
      public void mouseReleased() 
      { 
      mouseP = false; 
      } 



public class Mallet{ 
    PImage mallet1; 
    PImage mallet2; 
    float xPos1; 
    float yPos1; 

    public Mallet(PImage mallet1, float xPos1, float yPos1){ 

     this.mallet1 = mallet1; 
     this.xPos1 = xPos1; 
     this.yPos1 = yPos1; 
    } 

    public void drawMallet(PImage mallet1, float xPos1, float yPos1){ 
     image(mallet1, xPos1 - 40, yPos1 - 60); 
    } 
} 
public class Mole{ 
    PImage Mole; 
    float xPos; 
    float yPos; 
    boolean dead = false; 

    public Mole(PImage mole){  
     this.Mole = mole; 
     this.xPos = rnd.nextInt(950); 
     this.yPos = rnd.nextInt(750); 
    } 


    public void drawMole(){ 
     if (dead == true) { 
      this.xPos = rnd.nextInt(1000 - mole.width/2); 
      this.yPos = rnd.nextInt(800 - mole.height); 
      dead = false; 
     } 
     image(Mole, xPos, yPos);  
    } 

    public void collision(Mallet m){ 
     if(
       mouseP == true && 
     mouseX > xPos && mouseX < xPos + mole.width && mouseY > yPos && mouseY < yPos + mole.height){ 
      score ++; 
      dead = true; 
     } 
    } 
    } 
} 
+0

Ihr Problem wurde gelöst? –

Antwort

1

mousePressed Einmal nach jeder Presse und mouseRelease genannt Einmal nach jeder Veröffentlichung genannt. Wenn Sie einen booleschen Wert auf mousePressed und false auf mouseReleased setzen, werden natürlich Aktionen abhängig von Ihrer mousepP-Variable ausgelöst, bis Sie Ihre Taste loslassen.

Wenn Sie Sie Aktionen einmal aufgerufen wollen pro Presse warum nicht Sie gerade nutzen:

mousePressed() { 
    if(someKeyIsPressed) { 
     // fire hit or whatever you want 
    } 
} 

so etwas wie dieses. Aktionen in mousePressed werden nur einmal pro Druck ausgeführt. mouseClicked wird nach einer gedrückten Freigabe aufgerufen. Es könnte auch so funktionieren, würden die erwähnten Seelenlösungen von Gaurava Agarwal, aber für Spiele Actions, wenn Sie einen Knopf loslassen, ist seltsam und ungewöhnlich. Stellen Sie sich ein fps-Spiel vor, bei dem Sie schießen, wenn der Knopf losgelassen wird.