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

2013/12/7

How to make an Object bounce off the world left and right?

Lautima Lautima

2013/12/7

#
I cannot figure out how to get my code to do this without me pressing a key, I am a beginner and do not know how to implement this. Please help, here is my code 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. } }
Adept_Red Adept_Red

2013/12/8

#
Well your first problem is you don't have any code in the code you posted. XD To start, you need a method to move the the object. If you've done any tutorials, you've probably tried move() already. Instead familiarize yourself with setLocation(x,y). This will place the object at whatever coordinates you've set. a sudo example of a method might be...
 private void movement()
    {
        if (speed > 0 && getX() == getWorld().getLength() -1 ||
           (speed < 0 && getX() == 0)) 
           speed = -speed;
           setLocation(getX() +speed, getY()); 
    }
Essentially this uses a var speed and the objects location to test a collision with the edges of the world and then inverse the direction when it 'hits'. You should check out the Greenfoot API for details on how to use setLocation and other Greenfoot methods.
Lautima Lautima

2013/12/8

#
Thank you Adept! Yes, I did try move() haha and I was putting the code in at the top to show *Robot1* I appreciate the help, you taught me something :)
Adept_Red Adept_Red

2013/12/8

#
Glad to hear it! Keep at it.
Lautima Lautima

2013/12/8

#
I implemented this into my code and it says cannot find "speed" variable. I know I am missing something but I am not sure what
Adept_Red Adept_Red

2013/12/8

#
Haha, I said sudo code for a reason! :P It is not complete of course. Come on, this is an easy one though! You know speed is a variable right? So where is it declared? (I'll give you a hint, it's not.)
Lautima Lautima

2013/12/8

#
Yeah I know its Sudo haha! Um.. I am thinking along the lines of int= something I have no clue haha, another hint please!
Adept_Red Adept_Red

2013/12/8

#
Lol, sounds like you really need to go over the basics then. You're on the right track though. Have a look at this. http://www.tutorialspoint.com/java/java_variable_types.htm This will give you the basics on variables in java and some examples.
Lautima Lautima

2013/12/8

#
I got past one issue to find another I did this private void movement() { int speed; - I added this length = length + 9; if (speed > 0 && getX() == getWorld().getLength() -1 || (speed < 0 && getX() == 0)) speed = -speed; setLocation(getX() +speed, getY()); } } I get an error saying it cannot find symbol - variable length. My world is 600x600 but I don't think the world size is the problem
Super_Hippo Super_Hippo

2013/12/8

#
If you do not declare your variables, he does know what to do with it. What is the reason for this length? You should set the int speed to something else than 0. Your robot will move this amount of cells and if it is 0, he does not move.
Adept_Red Adept_Red

2013/12/8

#
Uhh... get.Length() is a method D: You dont need length=length + 9
danpost danpost

2013/12/8

#
Adept_Red wrote...
Uhh... get.Length() is a method D:
Yes. 'getLength()' is a method; but not of the World class and cannot be called on the world returned by 'getWorld()'. The World class has both 'getWidth()' and 'getHeight()' which return the world's dimensions. With you comparing to values along the horizontal, 'getWidth()' is what you should have there. 'speed' should be declared outside the method and given a value (other than zero). Actually, looking at your code, it appears you are trying to reverse the direction of the object when it reaches the left and right edges of the world. You should only change its value when an edge is encountered; at which time you just negate its value.
Super_Hippo Super_Hippo

2013/12/8

#
Now I found that getLength() too. I skipped that part of the code because I thought you would know, what you do. Actually it is always almost the same. Like danpost said, this method does not exist in the world class, use getWidth() instead.
Lautima Lautima

2013/12/9

#
You guys are going a bit over my head, remember I am VERY inexperienced. Let me rephrase and clear up what I am doing. - I have 2 robots. I want the robots to bounce from wall to wall going left to right - I am also trying to get the robots to drop "Bullet" to kill the "Ship" Thing is I have no idea how to do this, and I cannot find a tutorial to get an AI to do this. My assignment is due in 2 days so I really need help, Thanks
danpost danpost

2013/12/9

#
The AI is not that difficult. What you want is this: 'if the robot is moving right and hits the right wall OR if the robot is moving left and hits the left wall, then reverse its direction of movement.' From this, you can see you need a way to determine which direction it is moving. Since it is moving horizontally and only the x coordinate changes, an 'int' instance field, call it 'xSpeed', set to some non-zero value is all you would need. Then you can use 'xSpeed' to change to location of the robots, negate its value to change the direction of movement for the robots and check the condition of its value to see if it is greater than or less than zero to determine the current direction of movement for the robots.
You need to login to post a reply.