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

2013/12/5

How can I make an object bounce off the walls going left to right?

Lautima Lautima

2013/12/5

#
without me pressing a key. I am trying to get Robot1 and Robot2 to bounce off the walls going left and then bounce off the wall going right. { /** * Act - do whatever the Robot1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }
danpost danpost

2013/12/6

#
Basically, (if moving right and contacts right wall) or (if moving left and contacts left wall): turn around. You just have to turn what I gave here into Java code using the methods of the Actor class. (also you will need to be able to track the direction of movement -- an 'int' field for speed in the x direction; a positive speed means moving right and a negative speed means moving left).
Lautima Lautima

2013/12/6

#
@danpost Thanks for replying, My friends at school told me the same thing but I just do not understand how to corporate that into a code that Greenfoot can understand
Yogibaer Yogibaer

2013/12/6

#
I think you should look for correct syntax by yourself. Nevertheless here some hints how to behave by moving to the right ( its not working yet):
int speed;
int posX;

public void move()
 { posX = getX();
   if (( posX + speed)>("width of world"))
     { speed = -1*speed;}
  posX = posX+speed;
 setPosition(posX,getY());
 }
Yogibaer Yogibaer

2013/12/6

#
A good solution: look the thread: "Help! with switching Actor directions?" a few steps down. (postmaster, please delete my previous posting)
You need to login to post a reply.