タワー ディフェンス ゲームを作ろうとしていますが、下部にあるタワー ボタンの 1 つをクリックしてプレイ エリアにドラッグすると、新しいオブジェクト タワーが作成されるように作成するのに問題があります。arraylist を使用してみましたが、ドラッグして新しいタワーを作成するたびに、前のタワーが消去され、新しいタワーが画面に表示されたままになります。
いくつかのクラスがあるので、関連があると思われるものだけを投稿します。他に必要な場合は、お付けします。私の投稿の長さを前もって申し訳ありません。
これは、イベントがパッケージ 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;
}
}
ありがとうございました。