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

2017/10/31

Random Movement

Beamo Beamo

2017/10/31

#
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):
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;
    }
}
Any help would be appreciated! Thank you!
danpost danpost

2017/10/31

#
I do not see how the actor can do anything at all -- it should remain perfectly still as far as I can see. In each act step, the following should happen: * call move, which should do nothing since timer is not zero (it is initially set to 20 -- and never changes its value); * call shoot, which is not yet implemented; * call explode, which also is not yet implemented; and * call setRandomNumber, which returns a '1' or '-1' which nothing is done with. I would use one timer and the speed field. Have the timer decrease every act step. If the timer is positive, move the actor using the speed value. If the timer is zero, set speed to a random negative value. If the timer is negative, compare it to speed value; if matching, reset new speed and give timer a positive random value. Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int timer = 20;
int speed = 1;
 
public void move()
{
    timer--;
    if (timer > 0) move(speed);
    if (timer == 0) speed = -2-Greenfoot.getRandomNumber(30); // max -2; min -31
    if (timer < 0 && timer == speed)
    {
        speed = 1-2*Greenfoot.getRandomNumber(2); // 1 or -1
        timer = 2+Greenfoot.getRandomNumber(30); // min 2; max 31
    }
}
Beamo Beamo

2017/10/31

#
Yep, that seems to do it! Regarding the shoot and explode functions, I have yet to describe them as other classes will call them. And the setRandomNumber function was a solution to the problem i had attempted a while back. Thanks for providing me with the solution!
You need to login to post a reply.