Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Moving a game object up and down in Java

This topic is 1974 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}
 
 
Edited by warnexus

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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;
}
Edited by warnexus

Share this post


Link to post
Share on other sites
Your added up/down variables have no effect; look carefully at the code and see if you can identify why.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 

Edited by warnexus

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!