Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (497) Games in Android Showcase (114) games submitted by our members Games in WIP (563) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Increasing cordinates of an image?  (Read 1942 times) 0 Members and 1 Guest are viewing this topic.
wreed12345

JGO Knight

Medals: 24
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Posted 2012-12-31 17:26:52 »

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
 1  2  3  4 `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?

Mac70
 « Reply #1 - Posted 2012-12-31 17:29:34 »

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

Check out my Devblog!
davedes
 « Reply #2 - Posted 2012-12-31 17:30:26 »

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

 1  2  3  4  5  6  7  8  9  10 `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:
 1  2  3  4 `float x;... in frame ...    x += 0.5f;`

You might want to multiply it by the delta time for framerate-independent movement:
 1 `    x += 0.5f * Gdx.graphics.getDeltaTime();`

wreed12345

JGO Knight

Medals: 24
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #3 - Posted 2012-12-31 17:31:45 »

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?

Jimmt
« League of Dukes »

JGO Kernel

Medals: 131
Projects: 4
Exp: 3 years

 « Reply #4 - Posted 2012-12-31 17:32:58 »

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

JGO Knight

Medals: 24
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #5 - Posted 2012-12-31 17:34:40 »

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

Mac70
 « Reply #6 - Posted 2012-12-31 17:42:41 »

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

Check out my Devblog!
wreed12345

JGO Knight

Medals: 24
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #7 - Posted 2012-12-31 20:47:49 »

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

Mac70
 « Reply #8 - Posted 2012-12-31 21:01:50 »

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

Check out my Devblog!
ReBirth
 « Reply #9 - Posted 2012-12-31 23:48:49 »

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

Mac70
 « Reply #10 - Posted 2013-01-01 00:51:12 »

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

Check out my Devblog!
ra4king

JGO Kernel

Medals: 345
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #11 - Posted 2013-01-01 09:08:20 »

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

wreed12345

JGO Knight

Medals: 24
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #12 - Posted 2013-01-01 16:17:43 »

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

LunaticEdit

Senior Member

Medals: 8
Projects: 1

 « Reply #13 - Posted 2013-01-01 16:24:12 »

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

DrZoidberg

Senior Member

Medals: 15

 « Reply #14 - Posted 2013-01-01 16:32:55 »

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

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
ReBirth
 « Reply #15 - Posted 2013-01-02 11:00:37 »

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

Riven
« League of Dukes »

JGO Overlord

Medals: 799
Projects: 4
Exp: 16 years

 « Reply #16 - Posted 2013-01-02 12:00:33 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 BurntPizza (25 views) 2014-09-19 03:14:18 Dwinin (39 views) 2014-09-12 09:08:26 Norakomi (69 views) 2014-09-10 13:57:51 TehJavaDev (94 views) 2014-09-10 06:39:09 Tekkerue (48 views) 2014-09-09 02:24:56 mitcheeb (69 views) 2014-09-08 06:06:29 BurntPizza (51 views) 2014-09-07 01:13:42 Longarmx (38 views) 2014-09-07 01:12:14 Longarmx (44 views) 2014-09-07 01:11:22 Longarmx (40 views) 2014-09-07 01:10:19
 BurntPizza 36x Riven 18x Rayvolution 18x basil_ 16x ags1 16x princec 16x KevinWorkman 15x LiquidNitrogen 12x kevglass 11x deathpat 11x nsigma 11x theagentd 11x HeroesGraveDev 8x The Lion King 7x Gibbo3771 6x cylab 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org