-4
public class Game
{
    private EnemyShip enemy1;
    private EnemyShip enemy2;
    private EnemyShip enemy3;
    private PlayerShip player;

    /**
     * Initialises user's ship and guns
     */  
    public Game()
    {
        player = new PlayerShip();
        enemy1 = new EnemyShip();
        enemy2 = new EnemyShip();
        enemy3 = new EnemyShip();

Can I make this enemy1,2,3 to be an array? I want to use enemy on a loop.

Or is there a way to make a loop for variable that is not an array? Like if I run the loop enemy1 will increment and become enemy2.

JJJ
  • 32,902
  • 20
  • 89
  • 102
trojan
  • 9
  • 4
  • im using java. sorry... – trojan Oct 16 '15 at 16:09
  • 1
    Ok, why couldn't you make it an array? Did you try it? – JJJ Oct 16 '15 at 16:12
  • `EnemyShip[] enemies = new EnemyShip[*how many*];` – 3kings Oct 16 '15 at 16:13
  • i tried... and i got this "int cannot be dereferenced" – trojan Oct 16 '15 at 16:14
  • Short answer: Yes, you can. `EnemyShip[] enemy = new EnemyShip[3];` there it is. – Frakcool Oct 16 '15 at 16:14
  • ok so ill make it to an array in the class? or in the constructor? – trojan Oct 16 '15 at 16:15
  • in the class you want to put.... `EnemyShip[] enemy;` and in the constructor put `enemy = new EnemyShip[3];` – 3kings Oct 16 '15 at 16:15
  • the EnemyShip is from another class.. so im soooo confused.. i can make an array like int [] enemy {........}; so for the one under the class it will remain the same? then the public Game() EnemyShip[] enemy = new EnemyShip[3];? is it correct? – trojan Oct 16 '15 at 16:18
  • Also this might be a duplicate question of: [Assigning variables with dynamic names in java](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) – Frakcool Oct 16 '15 at 16:28

2 Answers2

1
EnemyShip[] enemies = new EnemyShip[3];
for (int i=0; i<enemies.length; i++) { enemies[i] = new EnemyShip(); }

will give you an array of 3 EnemyShip objects. If the loop you're looking for is one that gives you a different instance variable each iteration then no you can't do that; you need to have them in a collection. However you can simplify the loop if you don't care about the index:

for (EnemyShip enemy : enemies) {
    //do something with enemy object
}
jonk
  • 1,494
  • 11
  • 13
  • if you already have enemy1, enemy2 and enemy3 objects then you _can_ initialise the array to include them: `EnemyShip[] enemies = new EnemyShip[] { enemy1, enemy2, enemy3 };` ...if that's what you're asking – jonk Oct 16 '15 at 16:31
  • whoaaa nice!! thank you!! taht will save me a lot of time.. im totally new to java... and my teacher want us to make this assignment.. so its quite hard since i got 0 background in I.T. thank you! – trojan Oct 16 '15 at 16:37
  • glad I was able to help, if it answers your question could you mark my answer as accepted :) – jonk Oct 16 '15 at 16:41
  • btw, does it mean that i can use enemy1 and enemy[0]. they both are the same? like in some i use enemy1 and on other i use enemy[0] – trojan Oct 16 '15 at 17:12
  • Yes `enemy1` and `enemies[0]` will be references to the same object. Be aware that this will mean changes to members of one will be present in the other (since they are the same object), e.g. if you do something like `enemy1.setId(2)` this will mean `enemies[0].getId()` will now also return 2 – jonk Oct 16 '15 at 17:54
1

As others have suggested, just do:

EnemyShip[] enemies = new EnemyShip[3];

for (int i = 0; i < enemies.length; i++) { 

    enemies[i] = new EnemyShip(); 
}

However, I think you should reconsider using an array. Using an array limits you to having, at maximum, as many enemies as will fit into the array.

Do you really know that you want at most 3 enemies at compile-time?

Why not make things more flexible and use a List instead? This will allow you to track as many enemies as you want to create at run-time. So for example, if the player is doing well you create more, if the player is doing poorly you create less.

List<Enemy> enemies = new ArrayList<Enemy>(3); // 3 is the expected capacity

for (int i = 0; i < 3; i++) { 

    createEnemy();
}

... elsewhere...

public void createEnemy() {

    enemies.add(new EnemyShip()); 
}

Requirements change, particularly in game development. Try to avoid committing to things too early.

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245