Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) 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 2555 times) 0 Members and 1 Guest are viewing this topic.
wreed12345

JGO Knight

Medals: 25
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: 25
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: 159
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: 25
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: 25
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: 374
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: 25
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 Devvie

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 Devvie

Medals: 17

 « 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: 954
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.

 BurntPizza (28 views) 2015-04-23 03:42:11 theagentd (32 views) 2015-04-22 16:23:07 Riven (46 views) 2015-04-16 10:48:47 Duke0200 (57 views) 2015-04-16 01:59:01 Fairy Tailz (40 views) 2015-04-14 20:13:12 Riven (42 views) 2015-04-12 21:36:37 bus hotdog (59 views) 2015-04-10 02:39:32 CopyableCougar4 (64 views) 2015-04-10 00:51:04 BurntPizza (68 views) 2015-04-06 22:06:58 ags1 (66 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 16x wessles 15x kingroka123 11x alwex 11x 65K 11x Rayvolution 8x kevglass 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x Ecumene 7x ra4king 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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