• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nicholas Kong

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

1

Share this post


Link to post
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 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
1

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.

0

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
0

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

Share this post


Link to post
Share on other sites

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)

}


 

1

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.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

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?

0

Share this post


Link to post
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

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
0

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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
		SetXPosition(paddleArea.Right
			+ this->dimensions_.x);
	}
	// Else speed must be greater than 0, travelling right.
	else
	{
                // drag the ball out of the right paddle
		SetXPosition(paddleArea.Left
			- 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 this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0