嗯...我錯過了什麼嗎? 嘗試從ArrayList中繪製導彈對象,但在調用p.missilies.size()時不斷收到錯誤NullPointerException。導彈肯定存在,並在Player類中實例化,但是如何從ActionMissile.class獲得對它的訪問?從不同的類訪問ArrayList
ActionMissile類
public class ActionMissile {
public ArrayList<Missilies> missilies = new ArrayList<Missilies>();
public void paint(Graphics g, MainClass mc) {
url = mc.getDocumentBase();
missile = mc.getImage(url, "missile.png");
//missilies = p.getMissilies();
for (int i = 0; i < p.missilies.size(); i++) {
Missilies m = new Missilies(mc);
m = (Missilies) p.missilies.get(i);
g.drawImage(missile, x, y, mc);
}
}
主要類
ActionMissile a;
public void init()
{
setSize(800, 600);
p = new Player(this);
e = new Enemy(this);
m = new Missilies(this);
a = new ActionMissile();
public void paint(Graphics g){
p.paint(g, this);
e.paint(g, this);
if (!p.missilies.isEmpty())
a.paint(g, this);
}
Player類
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()){
case KeyEvent.VK_SPACE:{
if (lastKey == null || lastKey != e.getKeyChar()) {
lastKey = e.getKeyChar();
Missilies m = new Missilies(x,y);
m.fire();
missilies.add(m);
System.out.println("Missilies "+missilies);
'class ActionMissile'聲明瞭'p'在哪裏? –
'p'是什麼?它從未在'ActionMissile'中聲明。 –
p未定義。這是如何編譯的? – tristan2468