2013-06-26 40 views
-2

當我遇到一個不尋常的問題時,我正在關注一些關於Java的基本遊戲設計的教程。當我運行一個簡單的在屏幕上移動一個基於圖像的角色的小程序時,它不會讓我使用這些方法來改變圖像的位置。它在我的robot.update()方法中引發了一個NullPointerException,它處理圖像的移動。奇怪的是,如果我從Applet框架的下拉列表中選擇「重新啓動」選項,它將正常工作。有任何想法嗎?使用簡單的Java applet遊戲改變結果

主類

package Game; 

import java.applet.Applet; 
import java.awt.Color; 
import java.awt.Frame; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.net.URL; 

public class First extends Applet implements Runnable, KeyListener { 

private Robot robot; 
private Image image, character; 
private Graphics second; 
private URL base; 

@Override 
public void init() { 
    setSize(800,480); 
    setBackground(Color.BLACK); 
    setFocusable(true); 
    Frame frame = (Frame) this.getParent().getParent(); 
    frame.setTitle("First Game"); 
    addKeyListener(this); 

    try { 
     base = getDocumentBase(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     System.out.println("Image not accessible"); 

    } 
    character = getImage(base, "data/character.png"); 
    // TODO Auto-generated method stub 
    super.init(); 
} 

@Override 
public void start() { 
    Thread thread = new Thread(this); 
    thread.start(); 
    robot = new Robot(); 
    // TODO Auto-generated method stub 
    super.start(); 
} 

@Override 
public void stop() { 
    // TODO Auto-generated method stub 
    super.stop(); 
} 

@Override 
public void destroy() { 
    // TODO Auto-generated method stub 
    super.destroy(); 
} 

@Override 
public void run() { 
    // TODO Auto-generated method stub 
    while (true) { 
     robot.update(); 
     repaint(); 
     try { 
      Thread.sleep(17); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

public void update(Graphics g) { 
    if (image == null) { 
     image = createImage(this.getWidth(), this.getHeight()); 
     second = image.getGraphics(); 
    } 
    second.setColor(getBackground()); 
    second.fillRect(0, 0, getWidth(), getHeight()); 
    second.setColor(getForeground()); 
    paint(second); 

    g.drawImage(image, 0, 0, this); 
} 

@Override 
public void paint(Graphics g) { 
    // TODO Auto-generated method stub 
    g.drawImage(character, robot.getCenterX() - 61, 
      robot.getCenterY() - 63, this); 
    super.paint(g); 
} 

@Override 
public void keyTyped(KeyEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void keyPressed(KeyEvent e) { 
    switch (e.getKeyCode()) { 
    case KeyEvent.VK_UP: 
     System.out.println("Move UP"); 
     break; 
    case KeyEvent.VK_DOWN: 
     System.out.println("Move DOWN"); 
     break; 
    case KeyEvent.VK_LEFT: 
     robot.moveLeft(); 
     break; 
    case KeyEvent.VK_RIGHT: 
     robot.moveRight(); 
     break; 
    case KeyEvent.VK_SPACE: 
     robot.jump(); 
     break; 
    } 
    // TODO Auto-generated method stub 

} 

@Override 
public void keyReleased(KeyEvent e) { 
    switch (e.getKeyCode()) { 
    case KeyEvent.VK_UP: 
     System.out.println("Stop UP"); 
     break; 
    case KeyEvent.VK_DOWN: 
     System.out.println("Stop DOWN"); 
     break; 
    case KeyEvent.VK_LEFT: 
     robot.stop(); 
     break; 
    case KeyEvent.VK_RIGHT: 
     robot.stop(); 
     break; 
    case KeyEvent.VK_SPACE: 
     robot.stop(); 
     break; 
    // TODO Auto-generated method stub 

    } 
} 
} 

機器人類

package Game; 

public class Robot { 

private int centerX = 100; 
private int centerY = 382; 
private boolean jumped = false; 

private int speedX = 0; 
private int speedY = 1; 

public void update() { 

    if (speedX < 0) { 
     centerX += speedX; 
    } else if (speedX == 0){ 
     System.out.println("Do not scroll the background"); 
    } else { 
     if (centerX <= 300) { 
      centerX += speedX; 
     } else { 
       System.out.println("Scroll background here"); 
     } 
    } 

    if (centerY + speedY >= 382) { 
     centerY = 382; 
    }else{      
       centerY += speedY; 
      } 

    // Handles Jumping 
    if (jumped == true) { 
     speedY += 1; 

     if (centerY + speedY >= 382) { 
      centerY = 382; 
      speedY = 0; 
      jumped = false; 
     } 

    } 

    // Prevents going beyond X coordinate of 0 
    if (centerX + speedX <= 60) { 
     centerX = 61; 
    } 
} 

public int getCenterX() { 
    return centerX; 
} 

public void setCenterX(int centerX) { 
    this.centerX = centerX; 
} 

public int getCenterY() { 
    return centerY; 
} 

public void setCenterY(int centerY) { 
    this.centerY = centerY; 
} 

public boolean isJumped() { 
    return jumped; 
} 

public void setJumped(boolean jumped) { 
    this.jumped = jumped; 
} 

public int getSpeedX() { 
    return speedX; 
} 

public void setSpeedX(int speedX) { 
    this.speedX = speedX; 
} 

public int getSpeedY() { 
    return speedY; 
} 

public void setSpeedY(int speedY) { 
    this.speedY = speedY; 
} 

public void moveRight() { 
    speedX = 6; 
} 

public void moveLeft() { 
    speedX = -6; 
} 

public void stop() { 
    speedX = 0; 
} 

public void jump() { 
    if (jumped == false) { 
     speedY = -15; 
     jumped = true; 
    } 

} 

} 

這是錯誤消息我gettings

Exception in thread "Thread-3" java.lang.NullPointerException 
    at Game.First.run(First.java:65) 
    at java.lang.Thread.run(Thread.java:722) 
+1

codess請!!! – stinepike

+0

您正在獲取NullPointerException,因此請指定更多詳細信息+代碼,以獲取此異常。 –

+0

我想你第一個問題是在這裏'框架=(框架)this.getParent()。getParent();'...小程序沒有框架 – MadProgrammer

回答

1

你應該運行的線程後初始化機器人對象先不要嘗試:

robot = new Robot(); 
Thread thread = new Thread(this); 
thread.start(); 

當你開始執行線程時,它可能以null機器人開始。

當你再次加載它的作品,那麼它被初始化。

+0

天才!感謝您的幫助和解釋!每一點洞察力都能幫助我學習! – bzhagar