I don't know the correct coding to be able to click in the world and add a new object. Can someone help?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Space. The final frontier. * * @author Michael Kšlling * @version 1.1 */ public class Space extends World { private String[] soundFiles = { "bass_guitar1" , "bass_guitar2" , "bass_guitar3" , "bass_guitar4" , "bass_guitar5" }; /** * Constructor for objects of class Space. * */ public Space() { super ( 960 , 620 , 1 ); createObstacles(); randomBodies( 5 ); } /** * Create a row of abstacles across the middle of our world. */ public void createObstacles() { int i = 0 ; while (i < soundFiles.length) { addObject ( new Obstacle (soundFiles[i] + ".wav" ), 80 + i* 200 , i* 100 + 100 ); i++; } addObject ( new Blinker(),Greenfoot.getRandomNumber( 940 ), Greenfoot.getRandomNumber( 620 )); } /** * Create a given number of bodies in the universe. Each body has a random initial state (size, * mass, direction, speed, color, location). */ public void randomBodies( int number) { while (number > 0 ) { int size = 20 + Greenfoot.getRandomNumber( 30 ); double mass = size * 7.0 ; int direction = Greenfoot.getRandomNumber( 360 ); double speed = Greenfoot.getRandomNumber( 150 ) / 100.0 ; int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); int r = Greenfoot.getRandomNumber( 255 ); int g = Greenfoot.getRandomNumber( 255 ); int b = Greenfoot.getRandomNumber( 255 ); addObject ( new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y); number--; } } } |