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

2018/4/26

Spawning Cars

LobsterL LobsterL

2018/4/26

#
i have called each timer, but the car1 is spawning randomly, the car3 is spawning randomly then too far apart and car2 and car4 donot spawn at all
LobsterL LobsterL

2018/4/26

#
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
{
    // int getX() x = Car3X;
    int fastcarTimer = 75;
    int slowcarTimer = 100;
    Lives life1 = new Lives();
    Lives life2 = new Lives();
    Lives life3 = new Lives();
    Frog frog = new Frog();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 900, 1); 
        addObject(new Finish(), 305, 32); // adding the finish lilypad
        addObject(new Finish(), 515, 32); // adding the finish lilypad
        addObject(new Finish(), 713, 32); // adding the finish lilypad
        addObject(new Finish(), 905, 32); // adding the finish lilypad
        addObject(new Finish(), 95, 32); // adding the finish lilypad
        addObject(new Car1(), 10, 630);
        addObject(new Car1(), 500, 630);
        addObject(new Car1(), 900, 630);
        addObject(new Car1(), 300, 630);
        addObject(new Car3(), 995, 537);
        addObject(new Car3(), 750, 537);
        addObject(new Car3(), 395, 537);
        addObject(new Car3(), 195, 537);
        addObject(new Car3(), 95, 537);
        addObject(new Car4(), 195, 736);
        addObject(new Car2(), 300, 736);
        addObject(new Car4(), 600, 736);
        addObject(new Car2(), 994, 736);
        addObject(new Car2(), 800, 736);
        addObject(new Frog(), 500, 850);
        showText("Lives : " , 40, 880);
    }
    public void act()
    {
        // if (Car3X == 0)
        // {
        // }
        randomslowCarSpawner();
        randomfastCarSpawner();
        createLives();
    }
    public void slowCarSpawn()
    {
    }
    public void fastCarSpawn()
    {
    }
    public void createLives()
    {
        if(frog.getLives() == 3)
        {
          addObject(life1, 90, 880);
          addObject(life2, 120, 880);  
          addObject(life3, 150, 880);  
        }else if(frog.getLives() == 2)
        {
            addObject(life1, 90, 880);
            addObject(life2, 120, 880);  
        }else if(frog.getLives() == 1)
        {
            addObject(life1, 90, 880);
        }
            // public void removeAtWorldEdge()
        // {
        // if (isAtEdge(Car3()))
        // {
            // removeObject(Car3());
            // spawnTimer();
            
        // }
        // }
        // public void spawnTimer()
       // {
        // spawnTimer++;
        // if (spawnTimer == 0)
        // {
            // addObject(new Car3(), 36, 36);
        // }
        // } 
    }
        public void randomfastCarSpawner()
    {
        fastcarTimer++;
        if(Greenfoot.getRandomNumber(75)<60&&fastcarTimer == 75)
        {
            addObject(new Car1(),10, 630);
        }
        if(Greenfoot.getRandomNumber(75)<50&&fastcarTimer == 75)
        {
            addObject(new Car3(),995, 537);
        }
        if(fastcarTimer == 76)
        {
            fastcarTimer = 0;
        }
    }
    public void randomslowCarSpawner()
    {
        slowcarTimer++;
        if(Greenfoot.getRandomNumber(100)<40&&slowcarTimer == 100)
        {
            addObject(new Car4(),995, 736);
        }
        if(Greenfoot.getRandomNumber(100)<30&&slowcarTimer == 100)
        {
            addObject(new Car2(),995, 736);
        }
    }
}
the world code
danpost danpost

2018/4/26

#
You are not resetting the slowcarTimer field like you are the fastcarTimer field.
LobsterL LobsterL

2018/4/26

#
Yes I saw that as i finished asking the question but it still is slow and is spawning in a pattern not randomly.
danpost danpost

2018/4/26

#
LobsterL wrote...
Yes I saw that as i finished asking the question but it still is slow and is spawning in a pattern not randomly.
I am quite sure they are spawning randomly. The problem is you're giving too good of a chance for spawning the cars that it almost appears not to be random. Change the ratio between the random values and what you compare them to. In particular, lower the comparing number or raise the random range of values (for all car types).
LobsterL LobsterL

2018/4/27

#
One more thing. how do i make so that is one car is touching another that it doesn't spawn?
danpost danpost

2018/4/27

#
LobsterL wrote...
One more thing. how do i make so that is one car is touching another that it doesn't spawn?
Override the addedToWorld(World) method in the class of that which is being spawned and have it remove itself if touching another.
LobsterL LobsterL

2018/4/29

#
how would I do that??
danpost danpost

2018/4/29

#
LobsterL wrote...
how would I do that??
What do you think an addedToWorld(World) method would look like (see Actor class API documentation for clues)?
You need to login to post a reply.