1

我正在尝试制作一个塔防游戏,但在制作时遇到了麻烦,因此当我单击底部的一个塔按钮并拖动到游戏区域时,它会创建一个新对象塔。我曾尝试使用数组列表,但每次我拖动以创建一个新塔时,前一个塔会擦除并且新塔保持绘制在屏幕上。

有几个课程,所以我只会发布我认为相关的课程。如果你需要其他的我可以放。提前抱歉我的帖子太长了。

这是处理事件的类 package addison;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class Controls extends JPanel implements MouseListener, MouseMotionListener,         ActionListener {

    Timer timer;

public static int x; //mouse's x pos
public static int y; //mouse's y pos
public static int iteration = 0;

public static String button = ""; //identifies pressed button

public static boolean pressed = false; //true if a button was pressed and not a random space
public static boolean created = false;

public static ArrayList<Tower> tower = new ArrayList<Tower>(); //arraylist of tower objects

public Controls() {

    timer = new Timer(5, this); //creates a 5ms timer
    timer.start(); //starts timer
    addMouseListener(this);
    addMouseMotionListener(this);
    setFocusable(true);
}

public void paintComponent(Graphics g) {

    super.paintComponent(g);

    Map.paint(g); //draws map
    Hud.paint(g); //draws HUD
    TextDisplay.paint(g); //displays text
}

public void actionPerformed(ActionEvent e) {

    repaint(); //redraws graphics every 5ms
}

@Override
public void mouseClicked(MouseEvent e) {


}

@Override
public void mouseEntered(MouseEvent e) {


}

@Override
public void mouseExited(MouseEvent e) {


}

@Override
public void mousePressed(MouseEvent e) {

    if (Hud.getButton()) {

        pressed = true;
    } else {

        pressed = false;
    }

    System.out.println(pressed);
}

@Override
public void mouseReleased(MouseEvent e) {

    x = e.getX(); //gets mouse's x pos
    y = e.getY(); //gets mouse's y pos

    if (pressed) { // if the button pressed was gun man

        tower.add(iteration, new Tower(TextDisplay.description, x, y, 100, 100, 25)); //add a new tower object to the end of the arraylist

        System.out.println(tower.get(0).x);

        created = true;
        pressed = false;
    }

    iteration++;
}

@Override
public void mouseDragged(MouseEvent e) {


}

@Override
public void mouseMoved(MouseEvent e) {

    x = e.getX(); //get mouse's x pos
    y = e.getY(); //get mouse;s y pos

    if (Hud.getButton()) {

        TextDisplay.hovering = true;
    } else {

        TextDisplay.hovering = false;
    }
}
}

package addison;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

这是绘制所有内容的类 public class Map {

public static Rectangle mouse; //location of cursor

public Map() {

}

public static void paint(Graphics g) {

    g.setColor(new Color(255, 0, 0)); //makes mouse hit box transparent
    mouse = new Rectangle(Controls.x, Controls.y, 5, 5); //create mouse hit box
    g.fillRect(mouse.x, mouse.y, mouse.width, mouse.height); //draw mouse hit box


    g.drawRect(0, 0, 800, 450); //play area

    g.drawRect(0, 0, 800, 500); //options area

    if (Controls.created) {

        g.fillRect(Controls.tower.get(Controls.iteration).x, Controls.tower.get(0).y, 50, 50);
    }
}
}

这是按钮所在的类:package addison;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Hud {

public static Rectangle gunman;
public static Rectangle laserTower;
public static Rectangle rocketLauncher;
public static Rectangle emBomb;
public static Rectangle soundGun;

public Hud() {

}

public static void paint(Graphics g) {

    gunman = new Rectangle(0, 450, 50, 50); //create gun man button
    g.fillRect(gunman.x, gunman.y, gunman.width, gunman.height); //draw gun man button

    g.setColor(Color.BLUE);
    laserTower = new Rectangle(50, 450, 50, 50); //create laser tower button
    g.fillRect(laserTower.x, laserTower.y, laserTower.width, laserTower.height); //draw laser tower button

    g.setColor(Color.CYAN);
    rocketLauncher = new Rectangle(100, 450, 50, 50); //create rocket launcher tower
    g.fillRect(rocketLauncher.x, rocketLauncher.y, rocketLauncher.width, rocketLauncher.height); //draw rocket launcher button

    g.setColor(Color.DARK_GRAY);
    emBomb = new Rectangle(150, 450, 50, 50); //creates em bomb button
    g.fillRect(emBomb.x, emBomb.y, emBomb.width, emBomb.height); //draw em bomb button

    g.setColor(Color.GREEN);
    soundGun = new Rectangle(200, 450, 50, 50); //create sound gun button
    g.fillRect(soundGun.x, soundGun.y, soundGun.width, soundGun.height); //draw sound gun button
}

public static boolean getButton() {

    if(Map.mouse.intersects(Hud.gunman)) {

        TextDisplay.description = "Gunman";
        return true;
    } else if (Map.mouse.intersects(Hud.laserTower)) {

        TextDisplay.description = "Laser Tower";
        return true;
    } else if (Map.mouse.intersects(Hud.rocketLauncher)) {

        TextDisplay.description = "Rocket Launcher";
        return true;
    } else if (Map.mouse.intersects(Hud.emBomb)) {

        TextDisplay.description = "E.M. Bomb";
        return true;
    } else if (Map.mouse.intersects(Hud.soundGun)) {

        TextDisplay.description = "Sound Gun";
        return true;
    } else {

        TextDisplay.description = "";
        return false;
    }
}
}

这是制作 Tower 对象的类:package addison;

public class Tower {

public static String type = ""; //type of tower e.g. gunman or laser tower
public static int x = 0;
public static int y = 0;
public static int range = 0; //tower range
public static int speed = 0; //tower speed
public static int sRange = 0; //tower's shrapnel range

public Tower(String a, int b, int c, int d, int e, int f) {

    type = a;
    x = b;
    y = c;
    range = d;
    speed = e;
    sRange = f;
}
}

谢谢你。

4

2 回答 2

1

因为您只在以下行的最后一次迭代中绘制了塔:

g.fillRect(Controls.tower.get(Controls.iteration).x, Controls.tower.get(0).y, 50, 50);

您需要在绘图功能中绘制所有塔

于 2012-08-11T02:06:51.803 回答
1

似乎您正在使用Control.iteration作为您的塔列表的键。但是,每次mouseReleased调用(无论是否创建塔),您都在递增iteration. 这意味着Controls.iteration您的绘图处理程序中的键永远不会引用新创建的塔。您也在使用Controls.tower.get(0).y- 为什么这与 x 坐标不同?

我希望我已经正确理解了这一点。如果您只是在绘制新创建的塔,为什么不在 Controls 中保留对它的引用?如果要全部绘制它们,则应从 0 循环到iteration - 1iteration++if (pressed)块内移动。

于 2012-08-11T02:11:13.530 回答