Hello,
I'm currently working trough the Greenfoot book-scenarios and experience a problem with exercise 6.38 (chapter 6, Newtons-Lab-3). The exercise tells me to write a code to add a planet in the world with a mouseclick. The planet should spawn at the position you click with the mouse.
I've written the code and it does what it should do (create a planet at the position you click with the mouse), but it does more... It also creates planets on the top-left corner on the screen. These planets are stuck there (they can't move) and, because of their mass, are forcing all other planets in the screen to that position. So I end up with all planets in the top-left corner of my screen.
My question is: Why does my code make more than 1 planet and why do they spawn at the top-left corner of the screen and are stuck there?
Thanks in advance,
PoDh34d
So here is there code I created for adding a planet with a mouseclick.
private void createPlanet() { int size = 20 + Greenfoot.getRandomNumber(30); double mass = size * 7.0; int direction = Greenfoot.getRandomNumber(360); double speed = Greenfoot.getRandomNumber(150) / 100.0; int r = Greenfoot.getRandomNumber(255); int g = Greenfoot.getRandomNumber(255); int b = Greenfoot.getRandomNumber(255); if (Greenfoot.mouseClicked(getWorld())) { MouseInfo mouse = Greenfoot.getMouseInfo(); int mouseX = mouse.getX(); int mouseY = mouse.getY(); getWorld().addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), mouseX, mouseY); } }