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

2016/11/6

Help with moving an object?

Astralman Astralman

2016/11/6

#
If the car is at the edge, move 30 to left. It moves once to left and stops. I'm working from another actor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void moveCars()
   {
       if(isAtEdge() == true)
       {
           World world = getWorld();
           List <Car> cars = world.getObjects(Car.class);
           for (Car car : cars)
           {
               {
                   car.move(30);
                   if (car.getX() == getWorld().getWidth()){
                       car.setLocation(0, getY());
                   }
                   if(car.isAtEdge()== true)
                   {
                       car.move(-30);
 
                   }
               }
danpost danpost

2016/11/6

#
Astralman wrote...
If the car is at the edge, move 30 to left. It moves once to left and stops. I'm working from another actor.
You have not given anything you would want the cars to do after moving 30 left. Btw, once they reach the edge, they are moving back 30; it is just that from then on, they move 30 forward -- are found at the edge again -- and move 30 back again all in the same act. The end result is that they end up at the same location they then started at, which is 30 pixels from the edge with the appearance of not moving at all. Oh, and 'getY' in the 'setLocation' call is not that of the car, as coded. However that statement will never execute as the car will never be at the x-coordinate being checked on in the 'if' condition. The last possible x-coordinate in an bounded world is one less than the world width.
Astralman Astralman

2016/11/8

#
I want it to move backwards 30 and keep going backwards until it reaches the edge and then go forward 30 until it reaches the edge. Does that make sense?
danpost danpost

2016/11/8

#
I think it would be quite easy to just have something like this:
1
2
car.move(30);
if (car.isAtEdge()) car.turn(180);
Astralman Astralman

2016/11/10

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void moveCars()
   {
       if(isAtEdge() == true)
       {
           World world = getWorld();
           List <Car> cars = world.getObjects(Car.class);
           for (Car car : cars)
           {
               {
                   car.move(30);
                   if (car.getX() == getWorld().getWidth()){
                       car.setLocation(0, getY());
                   }
                   if(car.isAtEdge()== true)
                   {
                       car.turn(180);
  
                   }
               }
That flips actor vertically but it does move to the left. How can I keep the orientation
Super_Hippo Super_Hippo

2016/11/10

#
This could work:
1
2
3
4
5
6
7
8
private int speed = 30;
 
//...
car.move(speed);
//...
//instead of line 16
speed = -speed;
//...
You need to login to post a reply.