2016-11-26 28 views
0

有點背景,我目前正在爲一個小遊戲和課後項目開展工作。目前它缺少一些功能,但其要點是你將一個不同類的「垃圾」放入一個容器中,並且如果不將正確類別的垃圾放入正確類型的容器中,將會丟失。代碼可以在這裏找到,它只是簡單地劃分在clases之間。你可以抓住它Here壓縮,或者它看過來:將對象移動到對象上時的錯誤

主類:

Juego j = new Juego(); 
String escena; 
void setup(){ 
    size(400,400); 
    escena = "inicio"; 

} 

void draw(){ 
    if (escena.equals ("inicio")){ 
    j.inicio(); 
    } else if (escena.equals("instrucciones")){ 
    j.inst(); 
    } else if (escena.equals("jugar")){ 
    j.Empezar(); 
    } else if (escena.equals("perdio")){ 
    j.perdiste(); 
    } 
} 

void mousePressed(){ 
    j.Agarrar(); 
} 

垃圾桶:

class Bas { 
    float posX = random(25, 325); 
    float posY = random(75, 300); 
    int tam = 50; 
    color amarillo = color (255, 255, 0); 
    color verde = color (160, 220, 50); 
    boolean appear = true; 
    boolean agarrado = false; 
    boolean perdido = false; 
    int col; 
    float move_x = 2.5; 
    float move_y = 1.5; 
    int xdirection = 1; // Left or Right 
    int ydirection = 1; // Top to Bottom 


    Bas(int c) { 
    col = c; 
    } 

    void dibujar() { 
    if (col == 1) { 
     fill(amarillo); 
     ellipse(posX, posY, tam, tam); 
    } 
    if (col == 0) { 
     fill(verde); 
     ellipse(posX, posY, tam, tam); 
    } 
    } 

    void cambiar() { 
    posX = round(random(50, 325)); 
    posY = round(random(75, 300)); 
    } 

    void appearOnScreen() { 
    appear = true; 
    cambiar(); 
    } 

    void dissapear() { 
    appear=false; 
    } 

    void agarrado() { 
    if (mousePressed && col == 1) { 
     this.posX = mouseX; 
     this.posY = mouseY; 
     if (mouseX > 0 && mouseX < 200 && mouseY > 350 && mouseY < 400) 
     { 
     cambiar(); 
     } 
     if (mouseX > 200 && mouseX < 400 && mouseY > 350 && mouseY < 400) { 
     perdido = true; 
     } 
    } 
    if (mousePressed && col == 0) { 
     this.posX = mouseX; 
     this.posY = mouseY; 
     if (mouseX > 200 && mouseX < 400 && mouseY > 350 && mouseY < 400) 
     { 
     cambiar(); 
     } 
     if (mouseX > 0 && mouseX < 200 && mouseY > 350 && mouseY < 400) { 
     perdido = true; 
     } 
    } 
    } 

    void translacion() { 
    this.posX = this.posX + (move_x*xdirection); 
    this.posY = this.posY + (move_y*ydirection); 

    if (this.posX > width-tam || this.posX < tam) { 
     xdirection *= -1; 
    } 
    if (this.posY > 300 || this.posY < 75) { 
     ydirection *= -1; 
    } 
    } 

    void cambiarPerdido(){ 
    this.perdido = false; 
    } 
} 

遊戲控制器:

class Juego { 
    Bas b[] = new Bas[6]; 
    Tachos t[] = new Tachos[2]; 
    int tam = 50; 
    float time; 

    Juego() { 
    for (int i = 0; i<6; i++) { 
     b[i] = new Bas(i%2); 
    } 
    for (int i = 0 ; i<2; i++){ 
     t[i] = new Tachos(i%2); 
     } 
    } 

    void Empezar() { 
    background(255); 
    for (int i = 0; i<6; i++) { 

     b[i].dibujar(); 
     b[i].translacion(); 
    } 
    for (int i = 0 ; i<2; i++){ 
     t[i].crearTachos(); 
    } 
    this.Agarrar(); 
    this.perdiste(); 
    } 

    void Agarrar() { 
    for (int i = 0; i<6; i++) { 
     if (dist(b[i].posX, b[i].posY, mouseX, mouseY) < tam/2) { 
     b[i].agarrado(); 
     } 
    } 
    } 

    void inicio() { 
    background (255); 
    noFill(); 
    rect(50, 70, 100, 50); 
    rect(50, 170, 130, 50); 
    fill(0); 
    textSize(20); 
    text("Comenzar", 50, 100); 
    text("instrucciones", 50, 200); 
    for (int i = 0; i<6; i++) { 
     b[i].cambiarPerdido(); 
     b[i].cambiar(); 
    } 
    if (mousePressed && mouseX>50 && mouseX < 150 && mouseY > 70 && mouseY < 120) { 
     escena = "jugar"; 
    } else if (mousePressed && mouseX > 50 && mouseX < 180 && mouseY > 170 && mouseY < 220) { 
     escena = "instrucciones"; 
    } 
    } 
    void inst() { 
    background (100); 
    fill(255); 
    rect (125, 275, 100, 50); 
    fill(0); 
    fill(0); 
    textSize(15); 
    text ("Volver", 150, 300); 
    text ("Utiliza el mouse para mover las bolsas de basura\n a sus correspondientes tachos\n si las colocas en el tacho equivocado pierdes!", 10, 100); 
    if (mousePressed && mouseX > 125 && mouseX < 225 && mouseY > 275 && mouseY < 325) { 
     escena = "inicio"; 
    } 
    } 

    void perdiste() { 
    for (int i = 0; i<6; i++) { 
     if (b[i].perdido == true) { 
     escena = "perdio"; 
     background(255); 
     noFill(); 
     rect (0, 300, 100, 50); 
     rect (300, 300, 100, 50); 
     fill(0); 
     textSize(40); 
     text("PERDISTE!", 100, 100); 
     textSize (20); 
     text("Volver", 15, 330); 
     text("Salir", 315, 330); 
     if (mousePressed && mouseX > 0 && mouseX < 100 && mouseY > 300 && mouseY < 350) { 
      escena = "inicio"; 
     } else if (mousePressed && mouseX > 300 && mouseX < 400 && mouseY > 300 && mouseY < 350) { 
      exit(); 
     } 
     } 
    } 
    } 
} 

廢紙簍容器:

class Tachos { 
    float posX; 
    float posY; 
    int col; 
    int tam = 200; 

    Tachos(int c) { 
    col = c; 
    } 

    public void crearTachos() { 
    if (col == 1) { 
     fill(255, 255, 0); 
     rect(0, height-50, tam, tam); 
    } 
    if (col == 0) { 
     fill(160, 220, 50); 
     rect(200, height-50, tam, tam); 
    } 
    } 

/* public void translacion() { 
    this.posX = this.posX + (move_x*xdirection); 
    this.posY = this.posY + (move_y*ydirection); 

    if (this.posX > width-tam || this.posX < tam) { 
     xdirection *= -1; 
    } 
    if (this.posY > height-tam || this.posY < tam) { 
     ydirection *= -1; 
    } 
    }*/ 
} 

我遇到的主要問題是,當我按下鼠標按鈕移動垃圾,然後將它移到另一塊垃圾上時,它們會堆疊在一起。這個想法是他們應該獨立於彼此的行動,但我似乎沒有得到它的工作。我很確定問題出在這裏:

void Agarrar() { 
    for (int i = 0; i<6; i++) { 
     if (dist(b[i].posX, b[i].posY, mouseX, mouseY) < tam/2) { 
     b[i].agarrado(); 
     } 
    } 
    } 

但我不知道如何解決它。任何人都可以幫我解決這個問題嗎?提前謝謝了。

回答

0

未來,請嘗試將問題縮小至MCVE

但看一段代碼,你隔離:

for (int i = 0; i<6; i++) { 
     if (dist(b[i].posX, b[i].posY, mouseX, mouseY) < tam/2) { 
     b[i].agarrado(); 
     } 
} 

我假定這遍歷所有的垃圾,如果鼠標在那個垃圾桶,它撿起來?而這個問題就是它拿起鼠標下的每一塊垃圾,而不是其中的一塊垃圾。

如果是這樣的話,那麼你可能只需使用break關鍵字打出來的循環,只要你抓住了一塊垃圾:

for (int i = 0; i<6; i++) { 
     if (dist(b[i].posX, b[i].posY, mouseX, mouseY) < tam/2) { 
     b[i].agarrado(); 
     break; 
     } 
} 

這將導致每當你搶循環停止執行第一塊垃圾。