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

2014/5/12

Making objects move different directions, and different speeds

kayuhnjayuhn kayuhnjayuhn

2014/5/12

#
I am making a frogger game and have come to a bit of a stand still, I am trying to get the lilypads to go in different directions, not all the same way across the screen, and also in different speeds, this is what I have so far, but it is glitchy and everytime it moves it visibly looks bad, here is my code for it so far...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static int direction;
  public void act()
  {
      if (atWorldEdge())
      {
          if(direction <0)
          {
              setLocation(605, getY());
          }
          else
          {
              setLocation(20, getY());
          }
      }
      move(Greenfoot.getRandomNumber(15));
  }
danpost danpost

2014/5/12

#
If you want your lilypads to move in different directions, then you need to remove the 'static' modifier from the 'direction' field and assign it some random value. The erratic movement is due to the constantly changing randomly generated speed value in your 'move' statement.
GRIFFIN GRIFFIN

2014/5/15

#
If you wanted to pre-set the speed you could pass in the variable through the constructor, and if not that, make a variable called random and set that in the constructor.
1
2
3
4
5
6
7
8
int random
 
public <classname> (int parRandom /* This will let you create a random number when you are adding the object*/){
      random = parRandom;
}
public void act(){
      move(random);
}
danpost danpost

2014/5/15

#
The following should produce smooth movement for the lilypads moving in both directions with different speeds; world-wrapping is also incorporated.
1
2
3
4
5
6
7
8
9
10
11
12
import greenfoot.*;
 
public class Lilypad extends Actor
{
    private int speed = Greenfoot.getRandomNumber(8)+1; // random speed
    private int direction = 1-2*Greenfoot.getRandomNumber(2); // random direction
 
    public void act()
    { // movement with world wrapping
        setLocation((getX()+getWorld().getWidth()+speed*direction)%getWorld().getWidth(), getY());
    }
}
kayuhnjayuhn kayuhnjayuhn

2014/5/19

#
Awesome the the code worked great! So now how would I do it if I wasn't trying to wrap around the world? I tried to play around with things but couldn't make it work.
danpost danpost

2014/5/19

#
kayuhnjayuhn wrote...
Awesome the the code worked great! So now how would I do it if I wasn't trying to wrap around the world? I tried to play around with things but couldn't make it work.
Ok. So, if you do not want them to wrap around the world. Then use this
1
2
3
4
5
6
7
8
9
10
11
12
import greenfoot.*;
 
public class Lilypad extends Actor
{
    private int speed = Greenfoot.getRandomNumber(8)+1;
    private int direction = 1-2*Greenfoot.getRandomNumber(2);
 
    public void act()
    {
        setLocation(getX()+speed*direction, getY());
    }
}
But I fear you will want more than just that.
kayuhnjayuhn kayuhnjayuhn

2014/5/19

#
ohhh okay thanks a ton!
kayuhnjayuhn kayuhnjayuhn

2014/5/20

#
So you were right, that wasn't exactly what I needed. This is kind of what I need but it clearly won't work or else I wouldn't be asking this.
1
2
3
4
5
6
7
8
9
10
11
private int speed = Greenfoot.getRandomNumber(8)+1
   private int direction = 1-2*Greenfoot.getRandomNumber(2); 
  
   public void act() 
   
       setLocation(getX()+speed*direction, getY());
       if (atWorldEdge())
           {
           turn(180);
           }
   }
I need it to turn completely around and keep on going once it hits the world edge. I tried it like this and a few other ways and every time it would just go to the edge and spin around in a loop and not come back. How would I fix this issue? And just so we clarify I did have it working with the atWorldEdge part before the whole setLocation... was added so I don't know why it won't work!
danpost danpost

2014/5/20

#
First, when using 'setLocation' to move, 'turn'ing is not going to change anything but the rotation of the actor's image (it will not influence the direction of movement unless you are using 'move(int)' to move). Next, 'turn'ing does not get the actor off the edge, so the next execution of the code will 'turn' the actor again... and again (hence the spinning). Using 'atWorldEdge' is too general for what you need here. You need a dual condition requirement for turning around when at either edge determined by both the current direction of movement and the current edge that it is at.
1
2
3
4
5
if ((direction == 1 && getX() == getWorld().getWidth()-1) ||
    (direction == -1 && getX() == 0))
{
    direction = -direction;
}
Basically, this says if the current set direction is toward the edge I am at, change directions; or more specifically if moving right and at right edge or if moving left and at left edge, change the direction of movement.
kayuhnjayuhn kayuhnjayuhn

2014/5/20

#
So then how would I make it turn around 180 degrees after it hits the side since turn doesn't work? I tried it with turn and all it does is move the other direction.
danpost danpost

2014/5/20

#
kayuhnjayuhn wrote...
So then how would I make it turn around 180 degrees after it hits the side since turn doesn't work? I tried it with turn and all it does is move the other direction.
I think you need to a bit more clear as to what you want. Are you wanting that the image of your actor face the opposite direction when his direction of movement changes? What are the possible ways of re-orienting the image to produce the change you want?
kayuhnjayuhn kayuhnjayuhn

2014/5/20

#
So if the image is of the top view of a car and it is driving to the left of the screen with the front of the car to the left of the screen and it hits the edge I want it to be able to turn around and go towards the right side of the screen like it is up there but then with the front of the car facing the right side also. And I'm not really sure of a way to re-orient an image.
danpost danpost

2014/5/20

#
If the image was the top view of a car then you can use 'move' and 'turn' methods instead of 'setLocation'. When using 'setLocation(getX()+direction, getY());':
1
2
3
4
5
6
if ((direction == 1 && getX() == getWorld().getWidth()-1) ||
    (direction == -1 && getX() == 0))
{
    direction = -direction;
    turn(180);
}
and when using 'move(1);':
1
2
3
4
5
if ((getRotation() == 0 && getX() == getWorld().getWidth()-1) ||
    (getRotation() == 180 && getX() == 0))
{
    turn(180);
}
kayuhnjayuhn kayuhnjayuhn

2014/5/20

#
Okay that first one worked, thanks!
You need to login to post a reply.