我目前正在使用Java和Codename One构建2D小行星游戏,重点关注OOP模式。但是,我遇到了麻烦,试图动态创建新对象(在我的情况下,我要添加的对象是一个超类GameObject)添加到ArrayList。正如你在GameWorld.java的void init()方法中看到的,我创建了一个包含GameObject类型的列表,其中包括Asteroids,Ships,Spacestations的子类。
程序要求在类中输入命令键盘,例如'a',然后应该将新的Asteroid(GameObject的子类)对象添加到ArrayList中。用户应该能够添加尽可能多的Asteroids,因为他们想要分配给ArrayList。
我的问题是:我如何做 没有 已经声明了一个名为AsteroidTest的变量类型Asteroid,就像我目前一直在做的那样我的GameWorld类的addAsteroid()函数?谢谢!
GameWorld.java
package com.mycompany.a1;
import java.util.ArrayList; //For ArrayList Usage
public class GameWorld {
public void init() {
ArrayList<GameObject> list = new ArrayList<GameObject>();
}
//other methods here to manipulate Game objects and data
public void addShip() {
Ship ShipTest = new Ship();
list.add(ShipTest);
System.out.println(ShipTest.getLocation());
}
public void addAsteroid(){
Asteroid AsteroidTest = new Asteroid();
list.add(AsteroidTest);
System.out.println(AsteroidTest.getLocation());
}
public void addSpaceStation(){
}
}
Game.java
package com.mycompany.a1;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.Label;
import com.codename1.ui.TextField;
import com.codename1.ui.events.ActionEvent;
import java.lang.String;
import com.codename1.ui.Form;
public class Game extends Form{
private GameWorld gw;
public Game (){
gw = new GameWorld();
gw.init();
play();
}
private void play(){
Label myLabel=new Label("Enter a Command:"); this.addComponent(myLabel);
final TextField myTextField=new TextField();
this.addComponent(myTextField);
this.show();
myTextField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
String sCommand=myTextField.getText().toString();
myTextField.clear();
switch (sCommand.charAt(0)){
case 's':
gw.addShip();
break;
case 'a':
gw.addAsteroid();
break;
case 'b':
gw.addSpaceStation();
break;
//add code to handle rest of the commands
} //switch
} //actionPerformed
} //new ActionListener()
); //addActionListener
} //play
//code to enter text field and receive keyboard input
}
GameObject.java 超类。子类包括小行星,船舶,导弹,SpaceStation
package com.mycompany.a1;
public abstract class GameObject {
private double x;
private double y;
public GameObject(){
x = 0;
y = 0;
}
public double getX(){
return x;
}
public void setX(double newX){
x = newX;
}
public double getY(){
return y;
}
public void setY(double newY){
y = newY;
}
public String getLocation(){
return "(" + x + ", " + y + ")";
}
}
Asteroid.java
package com.mycompany.a1;
import java.util.Random;
public class Asteroid extends MovableObject {
private Random rand = new Random();
public Asteroid() {
setX(rand.nextInt(1023) + rand.nextDouble());
setY(rand.nextInt(767) + rand.nextDouble());// TODO Auto-generated
constructor stub
}
}
答案 0 :(得分:1)
这很简单:
public void addAsteroid(){
this.list.add(new Asteroid());
}
但是如果你想打印位置,你必须在创建新的小行星时这样做,所以在构造函数中:
public GameObject(){
x = 0;
y = 0;
System.out.println("(" + x + ", " + y + ")");
}
此外:
ArrayList<GameObject> list
声明为List<GameObject>
以使用抽象非特定实现List<GameObject> list
声明为GameWorld类中的私有字段public void init()
java.util.concurrent.CopyOnWriteArrayList<E>
代替传统列表moveTo(double x, double y)
方法以更清楚地表达意图