Hi again! I've been trying to make an enemy move left or right (randomly generated), for a randomly generated length of time, and then stop for a random amount of time. The main problem I have encountered is the fact that when randomly generating a number, it would do it continuously making the actor move in the same spot over and over.
Here's my code for the actor (some functions haven't been made yet):
Any help would be appreciated!
Thank you!
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 60 61 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class startenemy here. * * @author (your name) * @version (a version number or a date) */ public class startenemy extends Actor { /** * Act - do whatever the startenemy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ int timer = 20 ; int walktime = 30 ; int walktimereset = 30 ; int timerreset= 30 ; int speed = 1 ; public startenemy(){ GreenfootImage idleOne = new GreenfootImage( "enemyidle1.png" ); GreenfootImage idleTwo = new GreenfootImage( "enemyidle2.png" ); setImage(idleOne); } public void act() { move(); shoot(); explode(); setRandomNumber(); } public void move(){ if (timer== 0 ){ walktime=walktimereset; walktime--; if (walktime!= 0 ){ move(setRandomNumber()); } if (walktime== 0 ){ timer=timerreset; } } } public void shoot(){ } public void explode(){ } private int setRandomNumber(){ int num = Greenfoot.getRandomNumber( 2 ); int numf= 0 ; if (num== 1 ){ numf= 1 ; } if (num== 2 ){ numf=- 1 ; } return numf; } } |