I am new on this platform of creating games, and to java in general. I have worked in C++ for a year at school, and they do look similar, but I have some problems. I do not understand why the following code doesn't want to randomly spawn "worms" at random times across the map. Please help me understand what I have done wrong.
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Worm here. * * @author (your name) * @version (a version number or a date) */ public class Worm extends Actor { /** * Act - do whatever the Worm wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { theyHatin(); spawnal(); } public void theyHatin() { if (Greenfoot.getRandomNumber( 100 )< 30 ) { move(Greenfoot.getRandomNumber( 20 )); } if (Greenfoot.getRandomNumber( 100 )< 5 ) { turn(Greenfoot.getRandomNumber( 90 )); } if (getX() <= 5 || getX() >= getWorld().getWidth() - 5 ) { turn( 180 ); } if (getY() <= 5 || getY() >= getWorld().getWidth() - 5 ) { turn( 180 ); } } public void spawnal() { if (Greenfoot.getRandomNumber( 100 ) == 1 ) { int x = Greenfoot.getRandomNumber( 400 ); int y = Greenfoot.getRandomNumber( 300 ); addObject( new Worm(), x, y); } } } |