所以我正在开展Space Invaders主题项目,我的大部分课程都已启动并运行,并开始使用动画。部分过程是船舶的武器。
我有一个武器类,如下所示(专注于构造函数):
/**
* @(#)Weapon.java
*
*
* @author Tristan Nel - 18179460
* @version 1.00 2015/3/4
*/
public class Weapon {
private String type;
private int damage;
private int rof; //rate of fire
private int orientation;
private int firingStage; //0 - not firing ; 1 - flash & recoil ; 2 - bullet
private String[] sprites; //Set of sprite image file names
public Weapon() {
}
public Weapon(String type, int damage, int rof, int orientation, int firingStage, String[] sprites)
{
this.type = type;
this.damage = damage;
this.rof = rof;
this.orientation = orientation;
this.firingStage = firingStage;
this.sprites = sprites;
}
//GET and SET Methods
public void setType(String type)
{
this.type = type;
}
public void setDamage(int damage)
{
this.damage = damage;
}
public void setROF(int rof)
{
this.rof = rof;
}
public void setOrientation(int orientation)
{
this.orientation = orientation;
}
public void setFiringStage(int firingStage)
{
this.firingStage = firingStage;
}
public void setSprites(String[] sprites)
{
this.sprites = sprites;
}
public String getType()
{
return this.type;
}
public int getDamage()
{
return this.damage;
}
public int getROF()
{
return this.rof;
}
public int getOrientation()
{
return this.orientation;
}
public int getFiringStage()
{
return this.firingStage;
}
public String[] getSprites()
{
return this.sprites;
}
}
在另一个类中,它处理游戏画面上要动画的所有元素,我希望有一个全局的硬编码武器类型数组,可以根据需要轻松访问。我试图在课程内容的顶部这样做:
/**
* @(#)GameScreen.java
*
*
* @author Tristan Nel - 18179460
* @version 1.00 2015/3/4
*/
import java.util.Scanner;
import java.io.*;
public class GameScreen {
private static final String HIGH_SCORE_FILE = "highScore.txt";
//Available Weapons
//UPDATED SINCE ORIGINAL POST
public static final Weapon[] WEAPONS = new Weapon[4];
WEAPONS[0] = new Weapon("Machinegun", 10, 20, 0, 0, {Graphics.MG_L_NORM, Graphics.MG_R_NORM});
WEAPONS[1] = new Weapon("Plasma MG", 20, 20, 0, 0, {Graphics.PMG_L_NORM, Graphics.PMG_R_NORM});
WEAPONS[2] = new Weapon("Photon Cannon", 40, 5, 0, 0, {Graphics.PC_L_NORM, Graphics.PC_R_NORM});
WEAPONS[3] = new Weapon("Alien Destabilizer", 60, 10, 0, 0, {Graphics.AD_L_NORM, Graphics.AD_R_NORM});
private Ship defender;
private Weapon equipped;
//private Invader[] aliens;
//private Bullet[] bullets;
private int score;
private int highscore;
private int lives;
public GameScreen() {
}
public GameScreen(Ship defender, int score, int lives)
{
this.defender = defender;
this.score = score;
this.lives = lives;
}
public void loadHighscore()
{
try
{
Scanner sc = new Scanner(new File(HIGH_SCORE_FILE));
this.highscore = Integer.parseInt(sc.next());
sc.close();
}
catch(FileNotFoundException fnf)
{
System.out.println(fnf);
this.highscore = 0;
}
}
public void saveHighScore(int highscore)
{
try
{
FileWriter write = new FileWriter(HIGH_SCORE_FILE);
PrintWriter pw = new PrintWriter(write);
pw.print(this.highscore);
pw.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
//GET and SET methods
public void setDefender(Ship defender)
{
this.defender = defender;
}
public void setScore(int score)
{
this.score = score;
}
public void setLives(int lives)
{
this.lives = lives;
}
public Ship getDefender()
{
return this.defender;
}
public int getScore()
{
return this.score;
}
public int getLives()
{
return this.lives;
}
}
这让我在每行尝试向数组中添加另一个元素时出现以下错误消息:
已更新 https://drive.google.com/file/d/0B7ye7Ul2JDG2NDFDRTJNM1FCd0U/view?usp=sharing
非常令人沮丧.. 我在某处读到你必须在方法中创建一个对象? (例如.main()) 但是我在我的驱动程序类中尝试过它并没有任何区别......
感谢任何帮助/建议(:
答案 0 :(得分:2)
有多个问题
您的课程正文中不能包含任意代码,例如: WEAPONS[0] =
来电。但是,您可以使用新的Type [] {}语法直接初始化数组。您也可以使用静态初始化程序static {},但不建议这样做。
此外,您需要通过new关键字使用构造函数,它不仅仅是一种方法,即new Weapon()
而非Weapon()
new String[]{{Graphics.MG_L_NORM, Graphics.MG_R_NORM}}
而非{Graphics.MG_L_NORM, Graphics.MG_R_NORM}
工作版
public static final Weapon[] WEAPONS = new Weapon[] {
new Weapon("Machinegun", 10, 20, 0, 0, new String []{Graphics.MG_L_NORM, Graphics.MG_R_NORM}),
new Weapon("Plasma MG", 20, 20, 0, 0, new String []{Graphics.PMG_L_NORM, Graphics.PMG_R_NORM}),
new Weapon("Photon Cannon", 40, 5, 0, 0, new String []{Graphics.PC_L_NORM, Graphics.PC_R_NORM}),
new Weapon("Alien Destabilizer", 60, 10, 0, 0, new String []{Graphics.AD_L_NORM, Graphics.AD_R_NORM})
};
答案 1 :(得分:0)
实际上我以前做错了,但看起来你需要使用像这样的new运算符来调用构造函数。
arrayName[0] = new Weapon();
答案 2 :(得分:0)
由于这些类看起来有点静态,因此需要考虑的其他内容就是使用枚举。当您必须搜索特定武器时,这将有助于避免并发症。更好的设计是使WeaponType枚举包含与武器相关的所有静态不可变数据,并让Weapon类包含所有状态数据。