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

2013/6/17

Randomly appearing objects

How do you get objects to randomly appear? The DoctorProfessor is out.
Gevater_Tod4711 Gevater_Tod4711

2013/6/17

#
If you just want to add an object anywhere in the world you can use:
addObject(new YourActor(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
If you mean something else you'll have to specify your question.
What i really want is for objectS to appear at random at the edge of the screen at random times. The DoctorProfessor is out!
danpost danpost

2013/6/18

#
// random chance to spawn
if (Greenfoot.getRandomNumber(100) == 1)
{
    // random top edge location
    int x = Greenfoot.getRandomNumber(getWidth());
    // spawn
    addObject(new ObjectS(), x, 0);
}
It compiles, but nothing happens... I want the object to appear at the right hand side of the screen. I have coded it so that it disappears when it hits the edge so that could be causing the problem. Any advice? The DoctorProfessor is out!
Gevater_Tod4711 Gevater_Tod4711

2013/6/19

#
If you want the objects to appear at the right hand side you need to change the code danpost already gave you like this:
    if (Greenfoot.getRandomNumber(100) == 0)  
    {  
        addObject(new ObjectS(), getWidth() - 1, Greenfoot.getRandomNumber(getHeight()));  
    }  
To fix the problem that the objects dissapear at the edge of the world you could either don't remove them at the right hand side or you can add the obejcts more left so that they don't get removed (instead of getWidth() - 1 use getWidth() - 25 or even more).
still nothing. I even removed the code that makes the meteors appear! The DoctorProfessor is out!
Gevater_Tod4711 Gevater_Tod4711

2013/6/20

#
Where did you put this code? If you put it in an act method of an actor that is not in the world or in a different world class it'll not work. Also if you don't call this method or this peace of code in any act method.
You need to login to post a reply.