我正在嘗試製作塔防遊戲,並且遇到了麻煩,因此當我單擊底部的其中一個塔形按鈕並拖動到遊戲區域創建一個新的對象塔。我曾嘗試過使用數組列表,但每次拖動前一個塔消除新塔時,屏幕上都會出現新塔。如何從java中的相同位置創建多個對象
有幾個類,所以我只會發布我認爲是相關的。如果你需要別人,我可以把它們。很抱歉,我的帖子的長度。
這是處理事件的類 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 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);
}
}
}
這就是按鈕的類的類: 包艾迪生;
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;
}
}
謝謝。
感謝響應,我試圖把\t \t'如果(Controls.created){ \t \t \t對(INT I = 0;我<= Controls.iteration;我++){ \t \t \t \t \t \t \t g.fillRect(Controls.tower.get(Controls.iteration).x,Controls.tower.get(Controls.iteration).y,50,50); '但是當我按下鼠標按鈕時它給了我一個錯誤。然後我得到這個錯誤:異常在線程「AWT-EventQueue-0」java.lang.IndexOutOfBoundsException:索引:2,大小:2 – arrowheadx16 2012-08-11 02:26:45
你的for語句應該是'for(int i = 0; i
2012-08-11 02:35:22
我仍然收到相同的錯誤消息,但不是2,它是1。當我宣佈迭代時,我將它設置爲零,因爲我知道數組從[0]開始,所以我不知道這是否是問題。 – arrowheadx16 2012-08-11 02:38:48