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

2017/12/15

how can i limit the ammount of actors are created when act =30 cycles

roonie01 roonie01

2017/12/15

#
public class MyWorld extends World
{
    // GLOBAL DECLARATIONS
    // ===================
    
    int actCount=0;
    
    // CONSTRUCTOR  (populate world here)
    // ===========
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 800, 1); 
        Ship s1;
        s1= new Ship();
        addObject(s1,400,600);

        Alien a1;
        a1= new Alien();
        addObject(a1,0,200);

    } // end constructor
    
    // ACT METHOD  (often left empty in World subclass)
    // ==========
    public void act()
    {
        actCount++;
        showText(""+actCount,50,500);
       
        
        //if the actCount is equal to 100 act cycles then add three aliens and add alien counter 
        //resets the actCounter
        if(actCount==100)
        {
            addObject(new Alien(),0,100);
            addObject(new Alien(),0,200);
            addObject(new Alien(),0,300);
            addObject(new forerunner(),799,100);
            addObject(new forerunner(),799,200);
            addObject(new forerunner(),799,300);
            actCount=0;
           
        }
        
        
        
    } // end act
    
} // end class
Super_Hippo Super_Hippo

2017/12/15

#
What do you mean with "when act =30 cycles"?
roonie01 roonie01

2017/12/15

#
I mean if act happens 30 times like if you copy past it in to world act it counts the amount of times act runs
Super_Hippo Super_Hippo

2017/12/15

#
Right now, you count the act cycles with the actCount variable and then when it ran 100 times, it creates the six objects and resets the timer. 100 act cycles are about two seconds at normal speed.
roonie01 roonie01

2017/12/15

#
I know that but I want it to only allow 30 actors to be added in total
Super_Hippo Super_Hippo

2017/12/15

#
You could try to use 'getObjects(null).size()<30' as a condition then.
roonie01 roonie01

2017/12/15

#
like this... because its giving me a message saying that illegal start of expression
  if(actCount==100)
        {
            addObject(new Alien(),0,100);
            addObject(new Alien(),0,200);
            addObject(new Alien(),0,300);
            addObject(new forerunner(),799,100);
            addObject(new forerunner(),799,200);
            addObject(new forerunner(),799,300);
            actCount=0;
            getObjects(null).size()<30>);  
        }
roonie01 roonie01

2017/12/15

#
under the 30
Super_Hippo Super_Hippo

2017/12/15

#
if (actCount==100)
{
    if (getObjects(null).size()<25)
    {
        //add your object
    }
}
Or if you want to get exactly 30 each time when there are at least 24, you check if there are 30, add one object, then check again and add the next and so on.
roonie01 roonie01

2017/12/15

#
so I don't under stand it wont just add 30 or 25it just keeps adding more
 if (getObjects(null).size()<25)
           {
                addObject(new Alien(),0,100);
                addObject(new Alien(),0,200);
                addObject(new Alien(),0,300);
                addObject(new forerunner(),799,100);
                addObject(new forerunner(),799,200);
                addObject(new forerunner(),799,300);
              
            }
Super_Hippo Super_Hippo

2017/12/15

#
Try to restart Greenfoot.
roonie01 roonie01

2017/12/15

#
thank you so much it works great
You need to login to post a reply.