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

2013/5/25

Movement Help

Robert2.0 Robert2.0

2013/5/25

#
How can I make an actor move up and down at random heights while moving at a constant speed of 1.
danpost danpost

2013/5/25

#
1) Will it always come down to the same level? if so, to what level? 2) Is there a minimum height that it is to achieve before coming back down? if so, what minimum height? 3) Will there be any delay between going up and coming down (or after coming down before going back up)? if so, how long a delay at both/which points?
Robert2.0 Robert2.0

2013/5/25

#
danpost wrote...
1) Will it always come down to the same level? if so, to what level? 2) Is there a minimum height that it is to achieve before coming back down? if so, what minimum height? 3) Will there be any delay between going up and coming down (or after coming down before going back up)? if so, how long a delay at both/which points?
I want the image to move up to a random height min: half a inch max: two inches and the same downwards I don't want the character to jump to the spot, the actor should move up to the random point
danpost danpost

2013/5/25

#
Please answer each question specifically. You generalizations are causing ambiguity in determining what you want.
Robert2.0 Robert2.0

2013/5/25

#
danpost wrote...
Please answer each question specifically. You generalizations are causing ambiguity in determining what you want.
Sorry for the confusion 1) The image could come down to same level since it is moving up and down randomly but doesn't have to. 2) maximum: 100 pixels; minimum 25 pixels 3)No there is now delay
danpost danpost

2013/5/25

#
Ok, first you need an instance int field to hold the changing limits. Then, you only need to check for that limit and when there, reverse the direction (which should also be held in an instance int field, storing one or negative one (so all you have to do is add that amount to the current y value) and get a new limit.
// with these instance fields
private int direction = -1;
private int cycles = Greenfoot.getRandomNumber(75)+25;
// using the 'move' method
private void move()
{
    setLocation(getX(), getY()+direction;
    cycles--;
    if (cycles == 0)
    {
        direction = -direction;
        cycles = Greenfoot.getRandomNumber(75)+25;
    }
}
You need to login to post a reply.