我有一个Player类
package game.player;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import game.input.InputHandler;
import game.player.Missile;
public class Player{
private BufferedImage sprite;
public int x, y, width, height;
private final double speed = 5.0d;
private Missile missile;
public Player(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
try{
URL url = this.getClass().getResource("ship.png");
sprite = ImageIO.read(url);
} catch(IOException e){}
}
public void keyPlayer(double delta, InputHandler i){
if(i.isKeyDown(KeyEvent.VK_D)){
if(this.x>=1240) return;
else this.x+=speed*delta;
}
if(i.isKeyDown(KeyEvent.VK_A)){
if(this.x<=0) return;
else this.x-=speed*delta;
}
if(i.isKeyDown(KeyEvent.VK_SPACE)){
Missile missile = new Missile(this.x);
}
}
public void update(InputHandler inputP){
keyPlayer(2.0, inputP);
if(missile != null) missile.update(0);
}
public void Draw(Graphics a){
a.drawImage(sprite,x,y,width,height,null);
if(missile!=null) a.drawImage(missile.spriteM,missile.x,missile.y,2,6,null);
}
public Rectangle getBounds(){
return new Rectangle(x,y,width,height);
}
}
和导弹类:
package game.player;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
public class Missile{
public BufferedImage spriteM;
public int x;
public int y=504;
private Timer timer2;
public Missile(int x){
this.x=x;
this.y=504;
this.spriteM=spriteM;
try{
URL url = this.getClass().getResource("missile.png");
spriteM = ImageIO.read(url);
} catch(IOException e){}
//Draw(game.ShooterGame.bbg);
System.out.println("Missile launched!");
}
/*public void Draw(){
g.drawImage(sprite,x,y,2,6,null);
}*/
public void update(int sec){
System.out.println("Updating missile");
timer2 = new Timer();
timer2.schedule(new Move(), sec*1000);
}
class Move extends TimerTask{
public void run(){
if(2>1){
y+=3;
}
}
}
}
玩家射击导弹,在屏幕上飞行,增加其“y”。我的问题是:由于某种原因,导弹没有出现在屏幕上(即使出现“导弹发射!”),我也无法弄清楚出了什么问题。
我正在使用JFrame和drawImage到backBuffer.getGraphics。我的意思是在Player类中绘制导弹,而不是在导弹类中。
感谢。
答案 0 :(得分:1)
我的第一个猜测是投放了IOException
并且未加载图片,或者您应该在repaint()
或您正在绘制图形的任何地方安排JPanel
。< / p>
您能告诉我们更多信息吗?