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

2015/11/26

How do you do this?

spider65 spider65

2015/11/26

#
How do you make something move right and left randomly?
danpost danpost

2015/11/26

#
spider65 wrote...
How do you make something move right and left randomly?
You need to be specific as to what you want. 'randomly' could apply to several aspects of its movement. You could mean turning around at different random locations; you could mean to move at random times back and forth between two distinct points; or you could mean something else. In any case, since there will be two distinct directions of movement horizontally, you will need at least one field for the direction (you could also have another one for the speed):
// instance fields
private int speed = 1; // assign appropriate value
private int direction = 1; // '1' for right; '-1' for left; and '0' for no movement
// within act or a method it calls
move(speed*direction);
Now, it is just a matter of controlling the value of the 'direction' field. There will probably be another field involved (possibly a destination x-coordinate value or a timer field) depending on what 'randomly' refers to. This other field will be used to determine when the value of the 'direction' field is to be changed.
spider65 spider65

2015/11/26

#
I mean moving at random times right and left between two distinct points.
danpost danpost

2015/11/26

#
spider65 wrote...
I mean moving at random times right and left between two distinct points.
Then, you only need choose a random delay time (the number of act cycles before the next move) and add a field to determine if the actor is moving or is waiting for the timer to exhaust itself. The two distinct points (x-coordinate value of those points) needs to be saved in the actor to maintain the range of movement for the actor (or collision detection can be used if the actor "bumps" into objects at both ends of its range of movement). There are other ways to produce the behavior you want. If the number of acts required to move the actor from one point to the other is known, the timer can be used for both counting down the number of moves made and for counting down the act cycles when not moving; also, the limits (x-coordinate of the end-points) of movement would not have to be saved (the only requirement would be in the set-up -- placement of actor and initial values of the fields). Try to get some code to work and we will try to help fix any issues with it, if needed.
spider65 spider65

2015/11/28

#
I mean moving across the x axis at different spots. Like sliding across the x.
danpost danpost

2015/11/28

#
spider65 wrote...
I mean moving across the x axis at different spots. Like sliding across the x.
It appears you are having difficulty explaining exactly what you want. Break the movement down to individual steps and list what should happen and when. By the way, the more you break it down, the easier it will be to code what you want to happen.
Game/maniac Game/maniac

2015/11/28

#
Is this what you are asking for?
int min_x = 0;
int max_x = 600;

int x = Greenfoot.getRandomNumber(max_x - min_x) + min_x;

public void act() {
    if(getX() == x)
        x = Greenfoot.getRandomNumber(max_x - min_x) + min_x;
    
    int speed = 5;

    if(x >= getX()+speed)
        setLocation(getX()+speed, getY());
    else if(x <= getX()-speed)
        setLocation(getX-speed, getY());
    else if(x > getX() || x < getX())
        setLocation(x, getY());
}
You need to login to post a reply.