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

2019/2/28

How to constantly spawn multiple actors without crashing Greenfoot

444Jam444 444Jam444

2019/2/28

#
I'm trying to code a game of Frogger, and I have everything just perfect, really... except for the constant flow of objects; cars, logs. I need to know how to make a constant stream of cars and logs.
/**MyWorld*/
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    Frog frog = new Frog();
    Car car1 = new Car(1);
    Car car2 = new Car(2);
    Car car3 = new Car(3);
    Car car4 = new Car(3);
    Car car5 = new Car(4);
    Log log6 = new Log(2);
    Log log7 = new Log(3);
    Log log8 = new Log(5);
    Log log9 = new Log(4);
    ObstacleDespawn despawn = new ObstacleDespawn();
    /** the lanes. lanes 1-5 spawn cars, lanes 6-9 spawn logs*/
    public int laneDelay1 = 5;
    public int laneDelay2 = 4;
    public int laneDelay3 = 4;
    public int laneDelay4 = 3;
    public int laneDelay5 = 2;
    public int laneDelay6 = 1;
    public int laneDelay7 = 2;
    public int laneDelay8 = 4;
    public int laneDelay9 = 6;
    public int lanes = 0;
    private int speed;
    public int i;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(572, 572, 1); 
        /** Each square measures 39p x 44p (width x height) */
        /** setBackgroundImage("Background template.jpg"); */
        prepare();
        /**
        *   below was an attempt I made at making Logs and Cars spawn infinitely without crashing Greenfoot. I've learnt to never mess with infinite 
        *   Java while loops.
        */
        for (i=1;i<10000;i++){
            spawnObjects();
            if (Greenfoot.isKeyDown("up")){
                i = 1;
            }
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(frog,286,550);
        addObject(despawn,0,286);
        addObject(car1,572,506);
        addObject(car2,572,462);
        addObject(car3,572,374);
        addObject(car4,572,330);
        addObject(car5,572,286);
        addObject(log6,572,198);
        addObject(log7,572,154);
        addObject(log8,572,110);
        addObject(log9,572,66);
    }
    
    /** My attempt to constantly spawn Obstacles (superclass of Log and Car), but it triggers once and doesn't spawn anything upon triggering*/
    public void spawnObjects()
    {
        
        if (laneDelay1 == 0){
            addObject(car1,572,506);
            laneDelay1 = laneDelay1 + 5;
        }
        if (laneDelay2 == 0){
            addObject(car2,572,462);
            laneDelay2 = laneDelay2 + 4;
        }
        if (laneDelay3 == 0){
            addObject(car3,572,374);
            laneDelay3 = laneDelay3 + 4;
        }
        if (laneDelay4 == 0){
            addObject(car4,572,330);
            laneDelay4 = laneDelay4 + 3;
        }
        if (laneDelay5 == 0){
            addObject(car5,572,286);
            laneDelay5 = laneDelay5 + 2;
        }
        if (laneDelay6 == 0){
            addObject(log6,572,198);
            laneDelay6 = laneDelay6 + 1;
        }
        if (laneDelay7 == 0){
            addObject(log7,572,154);
            laneDelay7 = laneDelay7 + 2;
        }
        if (laneDelay8 == 0){
            addObject(log8,572,110);
            laneDelay8 = laneDelay8 + 4;
        }
        if (laneDelay9 == 0){
            addObject(log9,572,66);
            laneDelay9 = laneDelay9 + 6;
        }
        
        delayTimer();
        
    }
    public void delayTimer(){
        laneDelay1--;
        laneDelay2--;
        laneDelay3--;
        laneDelay4--;
        laneDelay5--;
        laneDelay6--;
        laneDelay7--;
        laneDelay8--;
        laneDelay9--;
    }
}
code info: world dimensions are 572x572, each square measuring 44x44 (13 squares by 13 squares) The Car and Log Actor classes do not have much code in them, doubt inserting their code would help much (just movement and stuff) The layout of the world (graphic only, not individually classed) looks like: Y-coord Grass (end goal) 0-44 Water 44-88 Water 88-132 Water 132-176 Water 176-220 Grass 220-264 Road 264-308 Road 308-352 Road 352-396 Grass 396-440 Road 440-484 Road 484-528 Grass (start) 528-572 If you need any more details, let me know.
444Jam444 444Jam444

2019/2/28

#
I am on the clock here, so the earlier the better. Preferably within < 1 week...
danpost danpost

2019/2/28

#
Your action code, lines 51 thru 56, belongs in an act method. The act method provides the looping, so you can remove the for loop and just move what is inside it to the act method. You will need to call the delayTimer method from there also.
444Jam444 444Jam444

2019/2/28

#
I know they belong in an act() method, but I have put them in the MyWorld() class because I don't currently know how to spawn an Actor from the Actor's code. Is it something like (in the Actor's Act() method:) MyWorld().addObject(Car.class,getX(),getY())? I honestly don't know. Only had about 20 hours experience or less with Java (but I'm not new to coding. Just Java/Greenfoot). It would be great if you could somehow walk me through this process of spawning an actor through its own Act() method.
444Jam444 444Jam444

2019/2/28

#
Thanks for the quick reply by the way. Unfortunately I had gone to bed shortly after posting.
danpost danpost

2019/2/28

#
444Jam444 wrote...
walk me through this process of spawning an actor through its own Act() method.
I am not quite sure what you mean by this. You cannot even call an act method on an object that was not previously created.
I know they belong in an act() method, but I have put them in the MyWorld() class
I do not see what the problem is. The World class has an act method also. It is just not given in the template for a new World subclass. You should probably look through the API documentation of World and Actor to see what methods are available for each of the classes.
444Jam444 444Jam444

2019/2/28

#
danpost wrote...
The World class has an act method also.
Ah, I didn't know. I just assumed that only Actors could act. Thanks, I will try this now.
444Jam444 444Jam444

2019/3/1

#
I'll have to get back to you in a few hours with the result.
444Jam444 444Jam444

2019/3/2

#
Thank you @danpost, it works now. I might have a different problem now, but I'll have a go at it myself first. Keep up the good work, and thanks again.
You need to login to post a reply.