java - 使用构造函数重载和带有继承的预先存在的静态对象

标签 java inheritance parameters constructor overloading

这是我关于《太空入侵者》项目的第二个问题。

我已经重新组织了我的代码以使用继承和ArrayList。 在我的项目中,类 Ship、Weapon 和 Bullet 扩展了类 Entity(其中实体是游戏屏幕上可能出现的任何“事物”,具有坐标、方向和由数组表示的 Sprite 集)文件名)

在处理游戏屏幕的类中,我有一组预先存在的武器,为了方便起见,可以直接使用它们的统计数据。 它们是使用标准 Weapon 构造函数创建的,其中包含对 Entity 的 super 构造函数调用。

我的问题出在 Weapon 的重载构造函数上: 我想将此武器构造函数与武器所附着的船舶相关联(注意,这是两个平行发射的独立武器,与船舶中心间隔开),以便自动创建武器,其属性与当前游戏场景匹配(飞船会跟踪要装载的武器,然后在每一帧的屏幕上绘制)。

以下是类(class):

父类(实体)

//IMMUTABLE
public class Entity extends Object
{

    //FIELDS
    private final double x;
    private final double y;
    private final double orientation;
    private final String[] sprites;

    //CONSTRUCTOR
    public Entity(double x, double y, double orientation, String[] sprites)
    {
        this.x = x;
        this.y = y;
        this.orientation = orientation;
        this.sprites = sprites;
    }

    //ACCESSORS
    public double getX()
    {
        return this.x;
    }

    public double getY()
    {
        return this.y;
    }

    public double getOrientation()
    {
        return this.orientation;
    }

    private String[] getSprites()
    {
        return this.sprites;
    }
}

子类,船舶和武器(我也有子弹,但如果我修复武器问题,它也会修复子弹)

发货:

//IMMUTABLE
public class Ship extends Entity
{

    //GLOBAL VARIABLES
    public static final double SHIP_MOVE_INT = 1;
    //100 hp, weapon 0, not moving, no thrusters
    public static final State defState = new State(100, 0, false, false, 0);

    //FIELDS
    private final State state;

    //CONSTRUCTOR
    public Ship(double x, double y, double orientation, String[] sprites, State state)
    {
        super(x, y, orientation, sprites);
        this.state = state;
    }

    //ACCESSORS
    public State getState()
    {
        return this.state;
    }

    //METHODS
    public void moveLeft()
    {
        if (this.x > 15*SHIP_MOVE_INT)
        {
            this.x -= SHIP_MOVE_INT;
        }
    }

    public void moveRight()
    {
        if (this.x < Graphics.X_SCALE - 15*SHIP_MOVE_INT)
        {
            this.x += SHIP_MOVE_INT;
        }
    }

    //Works, but revise
    public void invaderPattern(double gameClock)
    {
        double stage1Bound = 0.3*Graphics.X_SCALE;
        double stage2Bound = stage1Bound + 0.05*Graphics.Y_SCALE;
        double stage3Bound = stage2Bound + stage1Bound;
        double stage4Bound = stage3Bound + 0.05*Graphics.Y_SCALE;

        if (gameClock < stage1Bound)
        {
            //Move right
            this.state.setMovingRight(true);
            this.state.setMovingLeft(false);
            this.x += SHIP_MOVE_INT;
        }
        else if (gameClock >= stage1Bound && gameClock < stage2Bound)
        {
            //Move down
            this.state.setMovingRight(false);
            this.state.setMovingLeft(false);
            this.y -= SHIP_MOVE_INT;
        }
        else if (gameClock >= stage2Bound && gameClock < stage3Bound)
        {
            //Move left
            this.state.setMovingLeft(true);
            this.state.setMovingRight(false);
            this.x -= SHIP_MOVE_INT;
        }
        else
        {
            //Move down
            this.state.setMovingRight(false);
            this.state.setMovingLeft(false);
            this.y -= SHIP_MOVE_INT;
        }
    }
}

武器:

//IMMUTABLE
public class Weapon extends Entity
{

    //FIELDS
    private final String type;
    private final int damage;
    private final int rof; //Rate of fire
    private final int firingStage;

    //CONSTRUCTORS
    public Weapon(double x, double y, double orientation, String[] sprites, String type, int damage, int rof, int firingStage)
    {
        super(x, y, orientation, sprites);
        this.type = type;
        this.damage = damage;
        this.rof = rof;
        this.firingStage = firingStage;
    }

    public Weapon(double x, Ship defender, double orientation)
    {
        super(x, defender.getY(), orientation, GameScreen.WEAPONS[defender.getState().getWeapon()].getSprites());
        this.type =         GameScreen.WEAPONS[defender.getState().getWeapon()].getType();
        this.damage =       GameScreen.WEAPONS[defender.getState().getWeapon()].getDamage();
        this.rof =          GameScreen.WEAPONS[defender.getState().getWeapon()].getRof();
        this.firingStage =  GameScreen.WEAPONS[defender.getState().getWeapon()].getFiringStage();
    }
    //END OF CONSTRUCTORS

    //ACCESSORS
    public String getType()
    {
        return this.type;
    }

    public int getDamage()
    {
        return this.damage;
    }

    public int getRof()
    {
        return this.rof;
    }

    public int getFiringStage()
    {
        return this.firingStage;
    }

}

接下来的错误来自第二个 Weapon 构造函数中的“super”调用以及 this.后面的赋值调用,即整个事情不起作用哈哈 https://drive.google.com/file/d/0B7ye7Ul2JDG2cy0yak82eUZaclE/view?usp=sharing

对 GameScreen 类的引用(它本身将引用 Graphics) 这些类(class)是:

GameScreen(我仍然需要添加 Mutators)

//MUTABLE - NB
public class GameScreen
{
    //GLOBAL VARIABLES
    public static final String HIGH_SCORE_FILE = "highScore.txt";

    //FIELDS
    private Ship defender;
    private Weapon[] weapons;
    private ArrayList invaders;
    private ArrayList bullets;
    private int score;
    private int lives;
    private int highscore;
    private double gameClock;

    //AVAILABLE WEAPONS
    public static final Weapon[][] WEAPONS = new Weapon[][]
    {
        {
            new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[0], "Machinegun L", 10, 20, 0),
            new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[0], "Machinegun R", 10, 20, 0)
        },
        {
            new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[1], "Plasma MG L", 20, 20, 0),
            new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[1], "Plasma MG L", 20, 20, 0)
        },
        {
            new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[2], "Photon Cannon L", 40, 5, 0),
            new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[2], "Photon Cannon R", 40, 5, 0)
        },
        {
            new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[3], "Alien Destabilizer L", 60, 10, 0),
            new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[3], "Alien Destabilizer L", 60, 10, 0)
        }
    };

    //AVAILABLE BULLETS
    public static final Bullet[] BULLETS = new Bullet[] //Correspond to WEAPONS above
    {
        new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[0], WEAPONS[0][0].getDamage()),
        new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[1], WEAPONS[1][0].getDamage()),
        new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[2], WEAPONS[2][0].getDamage()),
        new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[3], WEAPONS[3][0].getDamage())
    };

    //CONSTRUCTOR
    public GameScreen(Ship defender, Weapon[] weapons, ArrayList invaders, ArrayList bullets, int score, int lives)
    {
        this.defender = defender;
        this.weapons = weapons;
        this.invaders = invaders;
        this.bullets = bullets;
        this.score = score;
        this.lives = lives;
        this.loadHighscore();
        this.gameClock = 0;
    }

    //METHODS
    public void clk()
    {
        this.gameClock++;
        //Should only be called when the game itself is being played, not menus
    }

    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);
        }
    }

    //ACCESSORS
    public Ship getDefender()
    {
        return this.defender;
    }

    public Weapon[] getWeapons()
    {
        return this.weapons;
    }

    public ArrayList getInvaders()
    {
        return this.invaders;
    }

    public ArrayList getBullets()
    {
        return this.bullets;
    }

    public int getScore()
    {
        return this.score;
    }

    public int getHighscore()
    {
        return this.highscore;
    }

    public int getLives()
    {
        return this.lives;
    }

    public double getGameClock()
    {
        return this.gameClock;
    }

}

图形:

//LIBRARY
public class Graphics
{

    //GLOBAL VARIABLES
    public static final int REFRESH_RATE = 20; //delay in milliseconds
    public static final double X_SCALE = 100;
    public static final double Y_SCALE = 100;
    public static final int X_SIZE = 512;
    public static final int Y_SIZE = 624;
    //Cycles of 4 stage motions
    public static final double gameClockMax = X_SCALE*(0.6) + Y_SCALE*(0.1);

    //SPRITES
    public static final String[][] SHIP_SPRITES =
    {
        {"BasicShip_TRANS.png"},    //Defender  [0][...]
        {"BasicInvader_TRANS.png"}  //Invader   [1][...]
    };

    public static final String[][] WEAPON_SPRITES =
    {
        {"MG_L_TRANS.png", "MG_R_TRANS.png"},   //Machine Gun           [0][...]
        {"PMG_L_TRANS.png", "PMG_R_TRANS.png"}, //Plasma Machine Gun    [1][...]
        {"PC_L_TRANS.png", "PC_R_TRANS.png"},   //Photon Cannon         [2][...]
        {"AD_L_TRANS.png", "AD_R_TRANS.png"}    //Alien Destabilizer    [3][...]
    };

    public static final String[][] BULLET_SPRITES =
    {
        {"MG_PROJ_TRANS.png"},
        {"PMG_PROJ_TRANS.png"},
        {"PC_PROJ_TRANS.png"},
        {"AD_PROJ.png"}
    };
    //END OF SPRITES

    //FUNCTIONS
    public static void drawMenu()
    {
        StdDraw.clear(StdDraw.GRAY);
        StdDraw.text(50, 80, "THE SWARM");
        StdDraw.text(50, 50, "P - Play");
        StdDraw.text(50, 40, "Q - Quit");
    }

    public static void init()
    {
        StdDraw.setCanvasSize(X_SIZE, Y_SIZE);
        StdDraw.setXscale(0.0, X_SCALE);
        StdDraw.setYscale(0.0, Y_SCALE);
        drawMenu();
    }

    public static void drawShip(int type, Ship ship) // type: 0 Defender , 1 Invader
    {
        if (type > 1 || type < 0)
        {
            System.out.println("Invalid ship type");
            return;
        }
        int hp = ship.getState().getHealth();
        if (hp > 80) StdDraw.picture(ship.getX(), ship.getY(), SHIP_SPRITES[type][0]);
        //TODO
    }

    public static double orientation(Ship defender) //Determine weapon orientation to mouse pointer direction
    {
        //Clockwise rotation thus 270 + theta

        if (defender.getX() < StdDraw.mouseX())
        {
            if (defender.getY() < StdDraw.mouseY())
            {
                return 270 + Math.toDegrees(Math.atan((StdDraw.mouseY() - defender.getY())/(StdDraw.mouseX() - defender.getX())));
            }
            else
            {
                return 270;
            }
        }
        else if (defender.getX() > StdDraw.mouseX())
        {
            if (defender.getY() < StdDraw.mouseY())
            {
                return (180) + (270 + Math.toDegrees(Math.atan((StdDraw.mouseY() - defender.getY())/(StdDraw.mouseX() - defender.getX()))));
            }
            else
            {
                return 90;
            }

        }
        else
        {
            return 0;
        }

    }

    public static void drawWeapons(Weapon[] weapons)
    {
        //Left
        StdDraw.picture
        (
            weapons[0].getX(),
            weapons[0].getY(),
            weapons[0].getSprites()[0],
            weapons[0].getOrientation()
        );
        //Right
        StdDraw.picture
        (
            weapons[1].getX(),
            weapons[1].getY(),
            weapons[1].getSprites()[1],
            weapons[1].getOrientation()
        );
    }

    public static void drawBullet(Bullet bullet)
    {
        StdDraw.picture
        (
            bullet.getX(),
            bullet.getY(),
            bullet.getSprites()[0],
            bullet.getOrientation()
        );
    }

    //Primary function
    public void animate(GameScreen gs)
    {
        //TODO
        //accept display of stats (hp, lives, score, etc)

        /* Order of drawing:
         * 1 - Background
         * 2 - Borders
         * 3 - Stats
         * 4 - Thrusters
         * 5 - Weapons
         * 6 - Ships
         * 7 - Bullets
         * 8 - Effects
         * 9 - Notifications (Combo indicators etc)
         */

        //1
        StdDraw.clear(StdDraw.GRAY);

        //5
        drawWeapons(gs.getWeapons());

        //6
        drawShip(gs.getDefender());

        for (int i = 0; i < gs.getInvaders().size(); i++)
        {
            gs.getInvaders().get(i).invaderPattern(gs.getGameClock());
            drawShip(gs.getInvaders().get(i));
        }

        //REFRESH RATE DELAY
        StdDraw.show(REFRESH_RATE);


    }

}

总而言之,我正在尝试重载武器构造函数,以便它使用现有的武器配置(在 GameScreen 中列出,使用第一个武器构造函数创建)进行设置,其中一个将根据船舶的武器类型进行选择状态。 编译器对此有很多提示,我正在尝试确定原因>.<

最佳答案

GameScreen.Weapons 是二维数组,定义如下:

public static final Weapon[][] WEAPONS

这意味着,您有武器类型的二维数组。

例如,如果您使用此GameScreen.Weapons[0],它将返回数组中的第一行,即武器的一维数组。并且数组没有像 .getDamage()

这样的方法

PS:我想你误解了什么是多维数组,我建议你看一些教程(也许图片就够了,很简单,二维数组就是矩阵)

关于java - 使用构造函数重载和带有继承的预先存在的静态对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29577153/

相关文章:

c++ - 如何从基类 C++ 复制字段

javascript - 调用带有绑定(bind)到转发器的参数的 javascript 函数

java - jsp页面无法加载资源

java - 添加文件夹中的文件列表

java - 尝试覆盖方法时奇怪的 Eclipse 行为

C - 将二维数组上的指针传递给函数

templates - Grails:如何将参数从自定义标记库传递到模板?

java - 在 Eclipse 中的 GaeNature、Google App Engine 中运行构建器时出错

java - 实现 Shape 接口(interface)的 boolean contains(Rectangle2D r) 方法

c++ - 继承不同类型的 vector 类