这不是我正在处理的整个程序,我可能需要编辑更多信息才能解决这个问题。 该程序创建一个窗口并用背景颜色填充它。然后我希望我的处理程序类创建两个玩家(方框)并将它们放在两个单独的坐标上。我收到错误"无法实例化类型播放器" ,但是Player是一个抽象类,不需要做什么?
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
public class Game extends Canvas implements Runnable {
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 *9;
private Thread thread;
private boolean running = false;
private Handler handler;
public Game (){
new Window (WIDTH, HEIGHT, "a game", this);
//above creates the window of the game
handler = new Handler();
handler.addObject(new Player(100, 100, ID.Player));
handler.addObject(new Player (200, 200, ID.Player));
//these two lines are marked as the truble,
// Cannot instantiate the type Player
}
///////
import java.awt.Graphics;
import java.util.LinkedList;
public class Handler {
LinkedList<GameObject> object = new LinkedList<GameObject>();
//creates a list called Linkedlist this contains all objects
public void tick(){
//for making the game tick
for (int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.tick();
}
}
public void render (Graphics g){
for (int i = 0 ; i <object.size();i++){
GameObject tempObject = object.get(i);
tempObject.render(g);
}
}
public void addObject (GameObject object){
this.object.add(object);
//this is supposed to be called from Game class, and is supposed to add
//one object to the list.
}
}
////
import java.awt.Graphics;
public abstract class GameObject {
protected int x, y;
protected ID id;
protected int velX, velY;
public GameObject(int x, int y, ID id){
this.x = x;
this.y = y;
this.id = id;
}
public abstract void tick();
//abstract means it has to be used in all classes
public abstract void render(Graphics g);
}
///////
import java.awt.Color;
import java.awt.Graphics;
public abstract class Player extends GameObject {
public Player(int x, int y, ID id) {
super(x, y, id);//cordinates and tag
}
public void tick(){
}
public void render(Graphics g){
//this are the two bockes I want to create in my window
g.setColor(Color.white);
g.fillRect(x, y, 32, 32);
}
}
答案 0 :(得分:1)
Player是一个抽象类,所以你不能这样做:
new Player(100, 100, ID.Player)