2016-11-26 76 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; 
     } 
} 

这将导致每当你抢循环停止执行第一块垃圾。