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

2018/7/1

Adding a object at the edge of the screen

grnft grnft

2018/7/1

#
How can I add a object at a random place, but at the edge of the screen. The object comes "from above/underneath/the side" of the world. When I use:
getWorld().addObject( new  Dementor(), getWorld().getWidth(), getWorld().getHeight()
The object appears only in the bottom right corner of the world. Thank you guys in advance!!
Super_Hippo Super_Hippo

2018/7/1

#
if (Greenfoot.getRandomNumber(2)==0) //from left or right
{
    getWorld().addObject(new Dementor(), Greenfoot.getRandomNumber(2)*(getWorld().getWidth()-1), Greenfoot.getRandomNumber(getWorld().getHeight()));
}
else //from top or bottom
{
    getWorld().addObject(new Dementor(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(2)*(getWorld().getHeight()-1));
}
grnft grnft

2018/7/1

#
You rescued my project!! Thank you so much!
You need to login to post a reply.