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

2023/1/9

Is it possible to create a timer or a block cade for the maximum number of objects?

Mordanto Mordanto

2023/1/9

#
is it possible to place a timer or a blockade for the maximum number of objects so that the game does not crash This is my code where this has to be implemented somewhere but I do not know how this works and greenfoot delay does not work either. if (done1= false){ int x; int y; x=0; y=0; if (!getWorld().getObjects(Test.class).isEmpty()){ // if a Test object is in the world Actor actor = (Actor)getWorld().getObjects(Test.class).get(0); // get reference to Test object x = actor.getX(); y = actor.getY(); getWorld().addObject(new Rocket(),x,y); } done1=true; } Greenfoot.delay(60); done1=false;
danpost danpost

2023/1/10

#
Mordanto wrote...
is it possible to place a timer or a blockade for the maximum number of objects so that the game does not crash This is my code where this has to be implemented somewhere but I do not know how this works and greenfoot delay does not work either. << Code Omitted >>
Using the Greenfoot.delay(int) method will temporarily pause your entire scenario for the number of time steps given. So ... no, it will not work for your purposes. You need an int timer field to control the spawning of rockets:
private int rocketSpawnTimer;

public void act()
{
    // control of spawning rockets at Test object location
    rocketSpawnTimer = (rocketSpawnTimer+1)%60;
    if (rocketSpawnTimer == 0)
    {
        if (!getWorld().getObjects(Test.class).isEmpty())
        {
            Actor actor = (Actor)getWorld().getObjects(Test.class).get(0);
            getWorld().addObject(new Rocket(), actor.getX(), actor.getY());
        }
    }
}
I am concerned about something. Why do you have this code in a Actor subclass? Seems like something the world should control. Actually, the coding would be much easier if it were in the World subclass. You could keep a reference to the Test object so that you would not have to find it every time a rocket needs spawned.
AlexMue AlexMue

2023/1/10

#
if i could get your email address i will send my file via email
danpost danpost

2023/1/11

#
AlexMue wrote...
if i could get your email address i will send my file via email
If you have errors, you can post your classes here. If not, you can upload the scenario here with the "Publish source code" checkbox checked.
You need to login to post a reply.