Jump to content

  • Log In with Google      Sign In   
  • Create Account

Moving a game object up and down in Java


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 17 January 2013 - 06:06 PM

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, 17 January 2013 - 06:11 PM.


Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 15699

Like
1Likes
Like

Posted 17 January 2013 - 06:56 PM

Look carefully at your if() statements and think about what they imply.

#3 Morphex   Members   -  Reputation: 298

Like
1Likes
Like

Posted 17 January 2013 - 07:25 PM

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 


#4 warnexus   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 17 January 2013 - 07:42 PM

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



#5 warnexus   Prime Members   -  Reputation: 1433

Like
1Likes
Like

Posted 17 January 2013 - 07:50 PM

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, 17 January 2013 - 07:56 PM.


#6 TheChubu   Crossbones+   -  Reputation: 4349

Like
2Likes
Like

Posted 17 January 2013 - 08:00 PM

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


#7 ApochPiQ   Moderators   -  Reputation: 15699

Like
0Likes
Like

Posted 17 January 2013 - 08:30 PM

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

#8 warnexus   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 17 January 2013 - 09:14 PM

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.



#9 warnexus   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 17 January 2013 - 09:40 PM

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, 17 January 2013 - 09:40 PM.


#10 ApochPiQ   Moderators   -  Reputation: 15699

Like
0Likes
Like

Posted 17 January 2013 - 10:38 PM


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.

#11 BeerNutts   Crossbones+   -  Reputation: 2944

Like
0Likes
Like

Posted 18 January 2013 - 09:09 AM

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)

#12 Morphex   Members   -  Reputation: 298

Like
1Likes
Like

Posted 18 January 2013 - 09:24 AM

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 


#13 warnexus   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 18 January 2013 - 11:13 AM


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, 18 January 2013 - 11:13 AM.


#14 warnexus   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 18 January 2013 - 11:16 AM

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

Any y position between 0 and including 200



#15 ApochPiQ   Moderators   -  Reputation: 15699

Like
0Likes
Like

Posted 18 January 2013 - 11:18 AM

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.

#16 slicer4ever   Crossbones+   -  Reputation: 3886

Like
0Likes
Like

Posted 18 January 2013 - 11:55 AM

<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, 18 January 2013 - 12:30 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#17 BeerNutts   Crossbones+   -  Reputation: 2944

Like
0Likes
Like

Posted 18 January 2013 - 12:36 PM

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)

#18 warnexus   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 18 January 2013 - 01:04 PM

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


Edited by warnexus, 18 January 2013 - 01:09 PM.


#19 FLeBlanc   Crossbones+   -  Reputation: 3101

Like
0Likes
Like

Posted 18 January 2013 - 01:32 PM

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.



#20 BeerNutts   Crossbones+   -  Reputation: 2944

Like
0Likes
Like

Posted 18 January 2013 - 01:44 PM

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)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS