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

2019/11/22

Add certain amount of objects

SemDeBakkero SemDeBakkero

2019/11/22

#
Hi, I want to add a certain amount (50) of actors. I already have the following in my world class:
int meteorAmount = 50;
public Dodge_Meteor()
   {    
       super(1184, 670, 1); 
       addObject(mainSpeler, getWidth()/2, getHeight()/2); //Speler toevoegen
       meteor = new meteor[meteorAmount];
       int y = Greenfoot.getRandomNumber(getHeight()); //en y is een random waarde -> ze spawner overal qua hoogte
       addObject(meteor, getWidth(),y);
        
    }
What am I doing wrong?
SailingStone SailingStone

2019/11/22

#
I'm not to certain, but I think the problem is that with the:
meteor = new meteor[meteorAmount];
line you are just defining what I'm assuming is a meteor array to have 50 slots that are all empty. Also, the addObject() method works only with Actor objects, not with Actor, so you were probably getting an error statement there. If that's the case, a way to make the world spawn 50 meteors and place them at a random y location is replace lines 6-8 with this:
for(int i = 0; i < meteorCount; i++){
    int y = Greenfoot.getRandomNumber(getHeight());
    addObject(new meteor(), getWidth(), y);
}
Hope that helped.
danpost danpost

2019/11/22

#
SailingStone wrote...
with the:
meteor = new meteor[meteorAmount];
line you are just defining what I'm assuming is a meteor array
Actually, that just assigns a new meteor array to a variable named meteor. However, if, how and where it is "defined" is unclear.
the addObject() method works only with Actor objects, not with Actor { array }, so you were probably getting an error statement there.
To be totally clear, not with Array objects.
If that's the case, a way to make the world spawn 50 meteors and place them at a random y location is replace lines 6-8 with this: << Code Omitted >>
Suggestion looks good. :) However, I seriously doubt that all the meteors are to appear right at the start. If they are to appear at random times, then an act method is needed:
public void act()
{
    if (Greenfoot.getRandomNumber(100) == 0 && meteorCount > 0)
    {
        meteorCount--;
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new meteor(), getWidth(), y);
    }
}
SemDeBakkero SemDeBakkero

2019/11/23

#
SailingStone wrote...
I'm not to certain, but I think the problem is that with the:
meteor = new meteor[meteorAmount];
line you are just defining what I'm assuming is a meteor array to have 50 slots that are all empty. Also, the addObject() method works only with Actor objects, not with Actor, so you were probably getting an error statement there. If that's the case, a way to make the world spawn 50 meteors and place them at a random y location is replace lines 6-8 with this:
for(int i = 0; i < meteorCount; i++){
    int y = Greenfoot.getRandomNumber(getHeight());
    addObject(new meteor(), getWidth(), y);
}
Hope that helped.
It kind of helped. But now I get an error that it doesn't know what meteorCount means (obviously). Should I just do:
int meteorCount;
? Already thanks tho!
SemDeBakkero SemDeBakkero

2019/11/23

#
oh wait i already see
SemDeBakkero SemDeBakkero

2019/11/23

#
Oh no they are all spawning at the same y. But y is random shouldn't they all spawn at a radom y??
SemDeBakkero SemDeBakkero

2019/11/23

#
This is my code now btw:
public class Dodge_Meteor extends World
{
   public Dodge_Speler mainSpeler = new Dodge_Speler(); //speler standaard toevoegen
   int numOfEnemy = Greenfoot.getRandomNumber(7) + 6; //aantal enemies toevoegen
   int EnemyCounter; //enemies tellen
   private int timer = 0; //timer instellen
   int starAmount = 250; //aantal sterren als variable instellen
   int meteorAmount = 50; //variable meteorAmount instellen als 50
   int i; //i als variable instellen
   int s; //s als variable instellen
   int meteorCount; //meteorCount als variable instellen
   int x = Greenfoot.getRandomNumber(getWidth()); //variable x instellen op random breedte
   int y = Greenfoot.getRandomNumber(getHeight()); //variable y instellen op random hoogte
   private Star[] stars;
   
   public Dodge_Meteor()
    {    
       super(1150, 610, 1); 
       addObject(mainSpeler, getWidth()/2, getHeight()/2); //Speler toevoegen
       
       
        
    }
    
   public void act()
   {
        timer++;
        if (timer == 1650) //als de timer 1650 is (staat ongeveer gelijk aan 30 secondes
        {
            Greenfoot.setWorld(new Home());  //ga dan naar home
        }
        for (Object obj : getObjects(Star.class)) ((Star) obj).move();
        makeStars();
        
        
        for(int u = 0; u < meteorAmount; u++)
        {
            addObject(new meteor(), getWidth(), y);
        }
    }


    private void makeStars()
    {
        stars = new Star[starAmount];  //stars instellen op nieuw star
        while (i < starAmount) {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            Star s = new Star();  
            addObject(s, x, y);  
            stars[i] = s;
            i++;
        }
    }

}
danpost danpost

2019/11/24

#
SemDeBakkero wrote...
Oh no they are all spawning at the same y. But y is random shouldn't they all spawn at a radom y??
The y field is initially assigned a random value. There is nothing in the for loop (lines 36 to 39) that would change that particular value assigned to y. Also, I doubt you want to have 3000 or so meteors created every second. That will cause almost instant lagging.
danpost wrote...
I seriously doubt that all the meteors are to appear right at the start. If they are to appear at random times, then an act method is needed:
public void act()
{
    if (Greenfoot.getRandomNumber(100) == 0 && meteorCount > 0)
    {
        meteorCount--;
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new meteor(), getWidth(), y);
    }
}
The same thing goes with your stars. Creating 250 of them every act frame is not going to work well; and the stars array (which apparently is not used at all) will only ever contain the last set of 250 created. You really need to explain exactly how you want all your stars and meteors to appear and behave; how many should be in the world at each point in time, when each should appear, etc.
SemDeBakkero SemDeBakkero

2019/11/25

#
Were (in which line) do you see it will spawn 3000 meteors. Because he does that now and I most certaintly don't want to spawn so many meteors?
Super_Hippo Super_Hippo

2019/11/25

#
You create 50 each act cycle (lines 36–39) and if you assume there are 60 act cycles per second, you get 50×60=3000.
SemDeBakkero SemDeBakkero

2019/11/25

#
Oh shit thanks!!
You need to login to post a reply.