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

2013/3/22

spawning new cars

JesusJesus JesusJesus

2013/3/22

#
Hey, im a very beginner with Greenfoot, and i have a (in think so) very simple question for you. Im creating a game, where you drive on a street, and have to move around other cars. I made a car, which stays on the same y point everytime in the game, and you can move its x position with the keys. the other cars move down, so it seems you are driving. I get it now that the other cars will remove if the are on the ground, but how can i get new cars which are coming in the game from the top, randomly on the x position? (Im very sorry for my bad English, I hope you can understand me, if not please ask Thank you, Jesus
Gevater_Tod4711 Gevater_Tod4711

2013/3/22

#
To spawn some cars randomly you need some code like this:
public void addCar() {
    getWorld().addObject(new Car(), Greenfoot.getRandomNumber(getWorld().getWidth(), 0);
}
this will spawn a car in your world everytime you call this method.
danpost danpost

2013/3/22

#
Then, to have cars spawn at random times, use:
int pct = 3; // percent chance of car spawning this act cycle
if (Greenfoot.getRandomNumber(100)<pct) addCar();
All code given by Gevater_Tod4711 and myself should be part of your world class code; the part I gave should be in the 'act' method or a method it calls. The revised 'addCar' method is:
private void addCar()
{
    addObject(new Car(), Greenfoot.getRandomNumber(getWidth()), 0);
}
JesusJesus JesusJesus

2013/3/22

#
 public void act() 
    {
        setLocation(getX(), getY() + 1);
        if(getY()>590)  
        {
            World world;
            world = getWorld();
            world.removeObject(this);
            return;
        }
    }    
    int spawn;
    public boolean atWorldEdge()
    {
        if(getX() > getWorld().getWidth()+500 - getImage().getWidth() || getY() > getWorld().getHeight() - getImage().getHeight())
        {
            spawn = Greenfoot.getRandomNumber(700);
            Blue Blue = new Blue();
            getWorld().addObject(Blue, spawn, 0);
            return true;
        }
        else
        {
            return false;
        }      
    
    }
thanks you two. maybe i can show you what ive tried already, but it doesnt work. Can you tell me the false? (Blue is the name of the car, because i have more other colored cars; my field is 600x400)
danpost danpost

2013/3/22

#
The only thing you need in the car classes is the following act method:
public void act()
{
    setLocation(getX(), getY()+1);
    if (getY()>590) getWorld().removeObject(this);
}
The code we supplied above should go in your world class.
JesusJesus JesusJesus

2013/3/22

#
okay thanks danpost. But it only removes and dont spawn again on top
JesusJesus JesusJesus

2013/3/22

#
Sry i hadnt seen the word at the end of your post. i tried it now, but it comes: "cannot find symbol - method getWidth()"
danpost danpost

2013/3/22

#
If you add the following to your act method of your world class, cars should spawn:
public void act()
{
    // some possible code you may already have
    spawnCar();
    // some more possible code you may already have
}
// add this method
private void spawnCar()
{
    int rand = Greenfoot.getRandomNumber(100);
    if (rand >=3) return;
    int spawnX = Greenfoot.getRandomNumber(getWidth());
    if (rand == 0) addObject(new Blue(), spawnX, 0);
    if (rand == 1) addObject(new Green(), spawnX, 0);
    if (rand == 2) addObject(new Red(), spawnX, 0);
}
JesusJesus JesusJesus

2013/3/23

#
Thank you very much danpost. You are great i works now :D
You need to login to post a reply.