Moving a game object up and down in Java

Started by
20 comments, last by Nicholas Kong 11 years, 2 months ago
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;
}
Advertisement
Look carefully at your if() statements and think about what they imply.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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

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

/* 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;
}

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 Making a Terrain Generator

Your added up/down variables have no effect; look carefully at the code and see if you can identify why.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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


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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement