Moving a game object up and down in Java
I will give you another hint, you need a state for your object, that defines what it is doing ( moving Up or Down)
Look carefully at your if() statements and think about what they imply.
First if implies the monster moving up in the screen
Second if implies the monster moving down in the screen
I will give you another hint, you need a state for your object, that defines what it is doing ( moving Up or Down)
Here's my code I using the feedback provided. =]
Follow your code assuming the monster's position is 200. What would happen?
Follow your code assuming the monster's position is 200. What would happen?
If the monster position is less than 200, the monster will move down. If the monster is over 200, the monster will move up.
Your added up/down variables have no effect; look carefully at the code and see if you can identify why.
because I am setting the value to be true to make the loop execute and then false when it is done. so the control logic did not work in this case
If the monster position is less than 200, the monster will move down. If the monster is over 200, the monster will move up.Follow your code assuming the monster's position is 200. What would happen?
Keep going, don't stop at one step.
Run through the code, say, 4 times in a row, starting with an initial value of 200.
At what point does the monster know when to stop moving up and start moving down?
Ok, I going to help you a bit more:
Lets say what do you want your object to do?
Your behaviour is something like this
Are we moving down?
{
Move down
If we are above 200, you want to make it move up. (Hint: State Change)
}
else{
Move Up
If we are below 0, you want to make it move Down. (Hint: State Change)
}
If the monster position is less than 200, the monster will move down. If the monster is over 200, the monster will move up.Follow your code assuming the monster's position is 200. What would happen?
Keep going, don't stop at one step.
Run through the code, say, 4 times in a row, starting with an initial value of 200.
Okay so I ran the code 4 times in a row and println the position.y to help me out with the problem. its move up at position.y = 200 and move down at position.y = 200
At what point does the monster know when to stop moving up and start moving down?
Any y position between 0 and including 200
I can be at y = 100 in two different ways: on my way up and on my way down. The y coordinate is not enough information to tell me which direction I need to move in.
Follow your code assuming the monster's position is 200. What would happen?
If the monster position is less than 200, the monster will move down. If the monster is over 200, the monster will move up.
Keep going, don't stop at one step.
Run through the code, say, 4 times in a row, starting with an initial value of 200.
Okay so I ran the code 4 times in a row and println the position.y to help me out with the problem. its move up at position.y = 200 and move down at position.y = 200
don't run the program and print it out, actually write out the values yourself, run the program in your mind, and follow it exactly, start at 0 with y, and follow your logic, 101 times, and you will see your problem
edit let's go a bit more, and do this:
edit: are you serious, the forum broke my post this badly =-\
At what point does the monster know when to stop moving up and start moving down?Any y position between 0 and including 200
Didn't that just answer your question?
When the monster is going down from 0 and crosses 200, it will then decide to go back up. When it moves 1 pixel up, it'll be between 0 and 200, and, according to you, it will start moving down again. 1 pixel later, it'll be below 200 and start moving up again. Rinse and repeat.
Doesn't that mean it will just bounce up and down over and back across y 200?
At what point does the monster know when to stop moving up and start moving down?Any y position between 0 and including 200
Didn't that just answer your question?
When the monster is going down from 0 and crosses 200, it will then decide to go back up. When it moves 1 pixel up, it'll be between 0 and 200, and, according to you, it will start moving down again. 1 pixel later, it'll be below 200 and start moving up again. Rinse and repeat.
Doesn't that mean it will just bounce up and down over and back across y 200?
that's exactly what is happening. i'm going to think about it some more to how to stop it from happening
They already hinted to you how to stop it from happening. You need to allow your object to "remember" which way it is traveling (up or down) as well as its position. Then when it crosses a threshold, it will switch direction.
They already hinted to you how to stop it from happening. You need to allow your object to "remember" which way it is traveling (up or down) as well as its position. Then when it crosses a threshold, it will switch direction.
Not only remembering the direction, but you need TWO threshold's, one for when you're moving down, and one for when you're moving up.
BTW, instead of using a boolean to set the direction, you could instead just change the sign of speed. So, instead of checking if (down == true), you'd check if (speed > 0) and when you cross the either threshold, you simply assign speed = -speed;
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.