1

我正在尝试在 Eclipse 中制作一个原始游戏,并且在编写将删除 GObjects 并使用 mouseClicked 方法更改其他人的属性的代码时遇到了一些麻烦。

问题似乎是在 mousePressed 方法中未识别私有即时变量。

有人可以告诉我我在这里做错了什么吗?我花了一整天的时间在这上面,我们将不胜感激。

亲切的问候,

梅胡尔

*/*
 * File: Linesv1.java
 * 07/08/2012
 * 
 */
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.MenuEvent;
public class Linesv1 extends GraphicsProgram {


    /** Width and height of application window in pixels */
    public static final int BACKGROUND_WIDTH = 400;
    public static final int BACKGROUND_HEIGHT = 600;
    /** Dimensions of game board (usually the same) */
    private static final int WIDTH = BACKGROUND_WIDTH;
    private static final int HEIGHT = BACKGROUND_HEIGHT;

    /** Dimensions of triangle*/
    private static final int LENGTH_OF_TRIANGLE_SIDE = 100;

    /** Dimensions of arc height*/
    private static final int ARC_HEIGHT = 100;

    /** Dimensions of radius of switches*/
    private static final int SWITCH_RADII = 5;


    // private instant variables

    private GObject switchA;

    private GOval switchB;

    private GObject triangle;


    private GObject bottomArc;


public void run() {
        addMouseListeners();
        setUpGame();
        }

public void setUpGame(){

    //add central triangle
    GPolygon triangle = new GPolygon (WIDTH/2,HEIGHT/2);
    triangle.addVertex(0,-LENGTH_OF_TRIANGLE_SIDE*2/3);
    triangle.addVertex(LENGTH_OF_TRIANGLE_SIDE/2,+LENGTH_OF_TRIANGLE_SIDE*1/3);
    triangle.addVertex(-LENGTH_OF_TRIANGLE_SIDE/2,+LENGTH_OF_TRIANGLE_SIDE*1/3);
    triangle.setFilled(true);
    triangle.setFillColor(Color.green);
    add(triangle);

    //add topArc
    GArc bottomArc = new  GArc (WIDTH/2-LENGTH_OF_TRIANGLE_SIDE/2, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-ARC_HEIGHT/2, ARC_HEIGHT,ARC_HEIGHT,0,-180);
    bottomArc.setFilled(true);
    bottomArc.setFillColor(Color.green);
    add(bottomArc);

    //add switches to the bottom of the triangle
    GOval switchA = new GOval (WIDTH/2-LENGTH_OF_TRIANGLE_SIDE/2-SWITCH_RADII, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-SWITCH_RADII,SWITCH_RADII*2,SWITCH_RADII*2);
    switchA.setFilled(true);
    switchA.setFillColor(Color.black);
    add(switchA);

    //add switches to the bottom of the triangle
    GOval switchB = new GOval (WIDTH/2+LENGTH_OF_TRIANGLE_SIDE/2-SWITCH_RADII, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-SWITCH_RADII,SWITCH_RADII*2,SWITCH_RADII*2);
    switchB.setFilled(true);
    switchB.setFillColor(Color.black);
    add(switchB);
    }
public void mousePressed(MouseEvent e) {
    findObject(e.getX(),e.getY());
    GObject check = findObject(e.getX(),e.getY());
    if (check!=null){
        remove(triangle);
        switchA.setColor(Color.cyan);
    }
}
private GObject findObject(int a, int b){

    if(getElementAt(a,b) != null) {
        return getElementAt(a,b);
    } else {
        return null;
    }
}
}*
4

1 回答 1

0

您尝试删除的三角形不是您添加的三角形。您在 setupGame() 中创建了一个名为 triangle 的本地 GPolygon 并添加了它,而不是私有成员。

 private GObject triangle; // this is uninitialized

 public void setUpGame(){

   GPolygon triangle = new GPolygon (WIDTH/2,HEIGHT/2);  // this is hiding your member variable

   // THIS IS LOCAL COPY, NOT MEMBER VARIABLE
   add(triangle);

然后稍后您尝试删除未初始化的成员变量,因此没有任何反应。您可能不想要三角形的单独本地副本。你可能想要

  public void setUpGame() {
    triangle = new GPolygon (WIDTH/2, HEIGHT/2);
    // ...
    add(triangle); // Now, you are adding the member
  }
于 2012-09-07T18:49:36.767 回答