我被卡住了導彈部分。 keyPressed
發生時,如何在Player
類中添加方法fire()
(來自Missile.java
)。一旦我按下空間導彈射擊並添加到導彈對象的ArrayList。但沒有發生。導彈不發射
private ArrayList<Missilies> missilies;
public Player (MainClass mc){
url = mc.getDocumentBase();
player = mc.getImage(url,"spaceship1.png");
}
public void paint(Graphics g, MainClass mc){
g.drawImage(player, x, y, mc);
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()){
case KeyEvent.VK_SPACE:{
// fire a missile;
missilies.add(new Missilies(x, y));
ArrayList ms = (Missilies).
missilies.add(new Missilies(getX(), getY()));
break;
}
和這裏的導彈類
public class Missilies {
// New missile object ////////
public Missilies(int x, int y){
this.x=x;
this.y=y;
visible=true;
}
// Moving missiles ////////
public void update(MainClass mc, Player p){
x +=missile_speed;
if (x>mc.getWidth())
visible = false;
}
// Painting ////////
public void paint(Graphics g, MainClass mc, Player p) {
for (int i = 0; i < missilies.size(); i++) {
Missilies m = (Missilies) missilies.get(i);
g.drawImage(missile, p.getX(), p.getY(), mc);
}
}
// Firing new missile ///////////
public void fire(Player p){
missilies.add(new Missilies(p.getX(), p.getY()));
}
// Action performed //////////
public void action(MainClass mc, Player p){
for (int i = 0; i < missilies.size(); i++) {
Missilies m = (Missilies) missilies.get(i);
if (visible == true)
update(mc, p);
else missilies.remove(i);
}
}
}
主要類
public void init()
{
setSize(800, 600);
p = new Player(this);
e = new Enemy(this);
}
{
while(running)
{
repaint();
p.update(this);
e.update(this, p);
try
{
Thread.sleep(20);
}
catch(InterruptedException e)
{
System.out.println("Error has occured");
}
}
}
public void update(Graphics g){
dbImage = createImage(800,600);
dbg = dbImage.getGraphics();
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paint(Graphics g){
//g.setColor(Color.BLACK);
p.paint(g, this);
e.paint(g, this);
}
}
**什麼**沒有發生?導彈的東西不會發生?它會觸發但不會添加到您的列表中?它脫離目標並且會殺死你的狗? –
_你的導彈必須有一些技術問題?? _ :) –
歡迎來到SO。請閱讀[問]。沒有人會通過代碼牆來調試。 – OldProgrammer