Increasing cordinates of an image?

Ok so i feel like I should know how to do this but heres my problem: I am increasing the X cordinates of an image by writing

float aX = 50;
		while(aX < 100){
			aX += .01;
		}

Normally this would work for me (im not sure if its the right way to do it but thats how I do it) but my computer is processing this so quickly that I cant notice the change in movement, I just notice the image at the final value. As i decrease the the amount that aX is increased by the same thing still happens until the game crashes… what should i do?

Is this in your game loop? If yes, just replace “while” with “if”.

If not, add thread.sleep().

The game loop is performed 60 times per second (or more). So what’s happening:

frame 0:
   x is 50;
   ..while loop..
   x is now 100
   img.draw(x, y)
frame 1:
   x is 50;
   ..while loop..
   x is now 100
   img.draw(x, y)

This is why the image will always be drawn at the same coordinates (x=100). Instead of using while loops within your game loop, you would simply increment the X position by a slight amount each frame. So your code changes to this:

float x;

... in frame ...
    x += 0.5f;

You might want to multiply it by the delta time for framerate-independent movement:

    x += 0.5f * Gdx.graphics.getDeltaTime();

oh wow its that simple. I just had to switch the if with the while. How do I know when to use either of them?

Since your entire main loop is like a big while loop I don’t think there is ever a need to use the while statement inside the loop, generally you want things to be cycling consistently.

Ok thanks man! And why wasnt the while loop working in this? too much looping or something ? lol

Because game must finish this loop before doing anything else. I will explain using picture:

http://img856.imageshack.us/img856/5093/loopg.png

thanks this really helped understand. if i wasnt making a game is there really a reason to use a while statement over an if?

In situations like this - only in another thread or using timer, otherwise the whole program will freeze.

Sorry but mistaking WHILE with IF is kinda fatal. Calm down and look again basic logic. No sarcasm, it’s serious :slight_smile:

Some tutorials for you:
http://www.tutorialspoint.com/java/java_loop_control.htm
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

I think this tutorials will help you to understand better. :wink:

STOP trying to make games and learn Java completely first! O___O

This is how I learn. You may not like it but I learn best with examples and trying things not just being taught something…Sorry lol

If you aren’t already, please use an IDE - I highly recommend IntelliJ IDEA, but eclipse will work too. If something isn’t working right, you need to put a breakpoint in your code, then step through it line by line. That will usually give you insight onto why X is happening. As you gain more experience, you start gaining the ability to simply read the code and run it through your mind’s debugger, which is never 100% accurate, but still works pretty well :smiley:

Instructions in a program are usually executed sequentially, one by one. A while loop is a loop. That means after the last instruction the processor jumps back to the start.
so
while(a < 100) {
do something
}
means
if(a >= 100) jump over the next 2 lines
do something
jump back by two lines

If you are learning new algorithm or mechanism like A*, dungeon generator, etc then yes. But you better at least can imagine how basic procedure like IF, WHILE, SWITCH, FOR running and the result in your head. It’s the best debugger tool.

Maybe sounding a bit depant, but ‘if/else/for/while’ is not quite debugging; it’s the basics of control flow.

If you mistake an ‘if’ for a ‘while’, you shouldn’t be writing any code. You should open up your study book (or buy it) and start from scratch. You cannot learn if & while from example.