Moving a game object up and down in Java

Started by
20 comments, last by Nicholas Kong 11 years, 3 months ago

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)

Advertisement

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


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

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

Any y position between 0 and including 200

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.

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

<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 =-\

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
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)

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;

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)

This topic is closed to new replies.

Advertisement