2

So I'm working on a Space Invaders theme project, and I have most of my classes up and running and have started on the animation. Part of the process is the ship's weapons.

I have a class for the weapons, as below (Focus on the constructor):

    /**
 * @(#)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;
    }

}

In another class, which handles all elements on the game screen to be animated, I want to have a global array of hardcoded Weapon types that can be accessed as needed without fuss. I have attempted to do so at the top of the contents of the class:

    /**
 * @(#)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;
    }

}

This gives me the following error messages on each line that I try to add another element to the array:

UPDATED https://drive.google.com/file/d/0B7ye7Ul2JDG2NDFDRTJNM1FCd0U/view?usp=sharing

It is highly frustrating.. I read somewhere that you have to create an object within a method? (Eg. main() ) But I tried that in my driver class and it made no difference...

Will appreciate any help/advice (:

9
  • Sorry the word wrap doesn't make the error report very readable. Please download the error.txt from my Google Drive to examine it correctly drive.google.com/file/d/0B7ye7Ul2JDG2NERKaE5wcVhNMlU/… Commented Mar 5, 2015 at 21:05
  • 1
    I think you must use the new keyword Commented Mar 5, 2015 at 21:07
  • I haven't looked carefully at the errors but I can tell you that an object declaration requires new, ie. new Weapon(stuff-goes-here). I would assume the amount of errors means this is the least of your problems. Commented Mar 5, 2015 at 21:08
  • Are you sure that passing {Graphics.MG_L_NORM, Graphics.MG_R_NORM} to String[] sprites is a valid way to do this? I don't know myself, just trying to think with you here. If you put those two in an array already and passed that as argument to the constructor... Commented Mar 5, 2015 at 21:17
  • Added in the new keyword @ValentinWaeselynck , it unfortunately didn't help Commented Mar 5, 2015 at 21:24

3 Answers 3

2

There are multiple issues

  • You cannot have arbitrary code in the body of your class, e.g. the WEAPONS[0] = calls. However, you can initialize the array directly using new Type[]{} syntax. You could also use a static initializer static {} but this is not recommended.

  • Also, you need to use the constructor via new keyword, it's not just a method, i.e. new Weapon() not Weapon()

  • You cannot declare arrays using {}, i.e. new String[]{{Graphics.MG_L_NORM, Graphics.MG_R_NORM}} not {Graphics.MG_L_NORM, Graphics.MG_R_NORM}

Working version

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})
};
Sign up to request clarification or add additional context in comments.

1 Comment

You are a wizard, worked perfectly. I was a bit dense to not realize that I had to use the new keyword for an array of Strings as a parameter. Many thanks
0

Actually I put it wrongly before but it looks like you need to call the constructor using the new operator like this.

arrayName[0] = new Weapon();

Comments

0

Since those classes seem somewhat static, something else to look into is using enums for this. This will help avoid complications when you have to search for a particular Weapon. A better design would be to have a WeaponType enum containing all the static immutable data in relation to a Weapon and have the Weapon class contain all the state data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.