Followers 0

# Moving a game object up and down in Java

## 21 posts in this topic

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
0

##### Share on other sites
Look carefully at your if() statements and think about what they imply.
1

##### 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)

1

##### Share on other sites
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

0

##### 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
1

##### Share on other sites

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

2

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

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

0

##### 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
0

##### 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.
0

##### Share on other sites

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

0

##### Share on other sites

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)

}

1

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

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

Edited by warnexus
0

##### Share on other sites
At what point does the monster know when to stop moving up and start moving down?

Any y position between 0 and including 200

0

##### Share on other sites
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.
0

##### Share on other sites

<blockquote class="ipsBlockquote" data-author="warnexus" data-cid="5022931" data-time="1358529193">
<blockquote class="ipsBlockquote" data-author="ApochPiQ" data-cid="5022769" data-time="1358483930">
<blockquote class="ipsBlockquote" data-author="warnexus" data-cid="5022756"><br />
<blockquote class="ipsBlockquote" data-author="TheChubu" data-cid="5022735">
<p>Follow your code assuming the monster's position is 200. What would happen?</p>
</blockquote>
If the monster position is less than 200, the monster will move down. If the monster is over 200, the monster will move up.<br />
&nbsp;<br />
<p>&nbsp;</p>
</blockquote>
<br />
Keep going, don't stop at one step.<br />
<br />
Run through the code, say, 4 times in a row, starting with an initial value of 200.</blockquote>
<p>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</p>
</blockquote>
<p>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</p>
<p>&nbsp;</p>
<p>edit let's go a bit more, and do this:</p>
<p>&nbsp;</p>
<p>

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

</div>
<div>*note the code tags, ['code] ['/code] without the '.</div>
<div>now let's say position.y = 199:</div>
<div>&nbsp;</div>
<div>

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

</div>
</div>
<div>&nbsp;</div>
<div>hopefully this makes it a bit more clear where your problem is.</div>

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

Edited by slicer4ever
0

##### Share on other sites
At what point does the monster know when to stop moving up and start moving down?

Any y position between 0 and including 200

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?

0

##### Share on other sites
At what point does the monster know when to stop moving up and start moving down?

Any y position between 0 and including 200

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

Edited by warnexus
0

##### Share on other sites

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.

0

##### Share on other sites
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;

0

##### Share on other sites

I used a similar approach to what BeerNutts suggested, in that I check for the sign of the speed against 0, and execute code as appropriate. Below is what happens when the auto-moving ball collides with a paddle, but the same can be applied to your boundsY scenario.

void Ball::OnPaddleCollision(sf::Rect<float> &paddleArea)
{
// Check ball moving left.
if(speed2D_.x < 0)
{
// drag the ball out of the left paddle
+ this->dimensions_.x);
}
// Else speed must be greater than 0, travelling right.
else
{
// drag the ball out of the right paddle
- this->dimensions_.x);
}
// Do everything else the same regardless of which paddle hit.
// Negate the sign of the speed, this will always occur so duplicated
// code does not need to be handled in the if-else.
speed *= -1;

return;
}

...

// Then in the Game.cpp, the ball will always move in this
// basic example. So always add the speed on, and the direction
// will change based in the sign of the speed variable.
ballObject.x += speed;

I always move the ball, but in my example, if a collision has occured (which is what you are wanting to do when the monster goes past a bound point), I drag it away from the point of collision, so that it doesn't count as a collision on the next frame. In the same method, I reverse the speed, so that as soon as collision has occurred with that wall, it will never happen again as it now moves in reverse.

To explain the movement more, this is how the movement method works.

Pos.X += Speed

Pos.x = Pos.x(200) + Speed(30)

Once it collides with the right paddle, then the speed is negated: Speed(-30)

Pos.x = Pos.x(200) + Speed(-30), this equates to Pos.x -= Speed.

Because of the way that addition works, if you increment a number by a negative number, then it effectively subtracts that number, meaning you don't cause any unnecessary duplication of code.

Sorry if this seems a bit garbled, I write this off the top off my head, if you need any more explanation please ask as I am happy to correct any obscurities.

Regards,

Stitchs.

0

##### Share on other sites

After a week of struggling, I finally got the monster to move up and down.   It was only possible because of everyone's feedback on the thread. I want to say Thanks!

0

## Create an account

Register a new account