Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Moving a game object up and down in Java

Started by Nicholas Kong Jan 18, 2013 at 12:06 AM 20 replies 13k views
Original Post
Nicholas Kong
Nicholas Kong
I got the game object to move down properly
but moving the game object up seems to be the hardest part to fix.
From what I am seeing the gameObject seems to be having an internal conflict
of where to go because when it moves up, it moves 1 pixel up and 1 pixel down pretty quickly
Here's my Java code:
private int speed = 2;
private int monsterYPosLimit = 200;
/*
* The monster continually descends
* all the way down to the screen
* at a certain point and then moves
* all the way back up the screen
* repeat this process
*
*/
if (position.y >= monsterYPosLimit)
{
position.y -= speed;
}
else if(position.y <= monsterYPosLimit)
{
position.y += speed;
}
EduardoMoura
EduardoMoura

I will give you another hint, you need a state for your object, that defines what it is doing ( moving Up or Down)

Check out my new blog: Morphexe 
Nicholas Kong
Nicholas Kong
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

Nicholas Kong
Nicholas Kong
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. =]

/* the appropriate speed for the monster
* while retaining the quality of the image
*/
private int speed = 2;
private int monsterYPosLimit = 200;
/*
* Control logic to handle the monster
* state of movement
*/
private boolean up = false;
private boolean down = false;
if(position.y <= monsterYPosLimit )
{
down = true;
if(down )
{
position.y += speed;
}
down = false;
}
else if (position.y > monsterYPosLimit)
{
up = true;
if(up )
{
position.y -= speed;
}
up = false;
}
TheChubu
TheChubu

Follow your code assuming the monster's position is 200. What would happen?

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"   My journals: dustArtemis ECS framework and 
ApochPiQ
ApochPiQ
Your added up/down variables have no effect; look carefully at the code and see if you can identify why.
Nicholas Kong
Nicholas Kong
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.

Nicholas Kong
Nicholas Kong
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

ApochPiQ
ApochPiQ

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.
BeerNutts
BeerNutts

At what point does the monster know when to stop moving up and start moving down?

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
EduardoMoura
EduardoMoura

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)

}


Check out my new blog: Morphexe 
Nicholas Kong
Nicholas Kong

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

Nicholas Kong
Nicholas Kong
At what point does the monster know when to stop moving up and start moving down?

Any y position between 0 and including 200

ApochPiQ
ApochPiQ
You're missing an important fact.

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.
slicer4ever
slicer4ever






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:






private int speed = 2;
private int monsterYPosLimit = 200;
 
/*
* The monster continually descends
* all the way down to the screen 
* at a certain point and then moves
* all the way back up the screen
* repeat this process
* 
*/
if (position.y >= monsterYPosLimit)
{
position.y -= speed;
}
else if(position.y <= monsterYPosLimit)
{
position.y += speed;
}



*note the code tags, ['code] ['/code] without the '.

now let's say position.y = 199:

 


//first iteration:
position.y = 199
if (199 >= 200) //false
{
 199 -= 2;
}
else if(199 <= 200) //true
{
 199 += 2;;
}
//second iteration:
position.y = 201;
if(201 >= 200){ //true
 201-=2;
}else if(201<=200){ //false(and not reached anyway)
 201+=2;
}
//third iteration:
position.y = 199
if(199 >= 200){ //false
 199-=2;
}else if(199<=200){ //true(again)
 199+=2;
}
position.y = 201
//repeat forever




 

hopefully this makes it a bit more clear where your problem is.

edit: are you serious, the forum broke my post this badly =-\

BeerNutts
BeerNutts
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?

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
Nicholas Kong
Nicholas Kong
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

FLeBlanc
FLeBlanc

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.

BeerNutts
BeerNutts
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;

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.