This site requires JavaScript, please enable it in your browser!
Greenfoot back
AlimMiah1
AlimMiah1 wrote ...

2014/12/17

Adding specific amount of enemies randomly different levels.

AlimMiah1 AlimMiah1

2014/12/17

#
I have a game which contains 3 levels. In each level I want to be able to add 5 enemies to the first, 8 to the second and 10 to the final level. I want them to be randomly placed. Is there a way to do this without adding the enemies manually but adding the specific amount enemies randomly each time the game is restarted?
Super_Hippo Super_Hippo

2014/12/17

#
It could be done like this. Of course you can change the values, I don't know how you want to place them randomly.
public Level(int lvl)
{
    super(/*...*/);
    //...
    
    int num;
    switch(lvl)
    {
        case 1: num = 5; break;
        case 2: num = 8; break;
        case 3: num = 10; break;
    }
    for (int k=0; k<num; k++) addObject(new ClassName(),10+ Greenfoot.getRandomNumber(getWidth()-10), 10+Greenfoot.getRandomNumber(getHeight()-10));
}
fejfo fejfo

2014/12/17

#
I would do it like this : Enmies enmieL1 = new Enmies; // if Enmies is the name of the class of youre Enmies Enmies enmieL2 = new Enmies; // if Enmies is the name of the class of youre Enmies Enmies enmieL3 = new Enmies; // if Enmies is the name of the class of youre Enmies public void Level1() { for(int i =0;i < 5; i++) { enmieL1 = new enmies(); addObeject(enmieL1,Greenfoot.getRandomNumber(worldWitch),Greenfoot.getRandomNumber(worldHight)); } } public void Level2() { for(int i =0;i < 8; i++) { enmieL1 = new enmies(); addObeject(enmieL1,Greenfoot.getRandomNumber(worldWitch),Greenfoot.getRandomNumber(worldHight)); } } public void Level3() { for(int i =0;i < 10; i++) { enmieL1 = new enmies(); addObeject(enmieL1,Greenfoot.getRandomNumber(worldWitch),Greenfoot.getRandomNumber(worldHight)); } }
davmac davmac

2014/12/17

#
fejfo, when you post here, there is a link just below the text box which says "Posting code? read this!". You need to click that link and read the linked page. You need to put proper code tags around code.
fejfo fejfo

2014/12/17

#
is this better ?
Enmies enmieL1 = new Enmies; // if Enmies is the name of the class of youre Enmies
Enmies enmieL2 = new Enmies; // if Enmies is the name of the class of youre Enmies
Enmies enmieL3 = new Enmies; // if Enmies is the name of the class of youre Enmies 

public void Level1() {
      for(int i =0;i < 5; i++) {
             enmieL1 = new enmies();
             addObeject(enmieL1,Greenfoot.getRandomNumber(worldWitch),Greenfoot.getRandomNumber(worldHight));
      }
}

public void Level2() {
      for(int i =0;i < 8; i++) {
             enmieL1 = new enmies();
             addObeject(enmieL1,Greenfoot.getRandomNumber(worldWitch),Greenfoot.getRandomNumber(worldHight));
      }
}

public void Level3() {
      for(int i =0;i < 10; i++) {
             enmieL1 = new enmies();
             addObeject(enmieL1,Greenfoot.getRandomNumber(worldWitch),Greenfoot.getRandomNumber(worldHight));
      }
}
davmac davmac

2014/12/17

#
is this better ?
I'm sure you can see for yourself ;) The problem now is that you copy-pasted from your previous post (the one without code tags), which means the array subscripts are missing. You need to use the original code, and put code tags around it.
AlimMiah1 AlimMiah1

2014/12/18

#
I have an error saying "cannot find symbol - variable worldWidth" ?
Super_Hippo Super_Hippo

2014/12/18

#
There are also some other mistakes in his post. worldWidth (or worldWitch) would be getWidth().
AlimMiah1 AlimMiah1

2014/12/19

#
I have amended the errors now and my game compiles. The only problem now is, each level doesn't have a specific amount of enemies, they just keep on spawning. Please help!
Super_Hippo Super_Hippo

2014/12/19

#
How did you implement it?
You need to login to post a reply.