Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Increasing cordinates of an image?  (Read 1622 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2012-12-31 18: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?

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

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

If not, add thread.sleep().

Check out my Devblog! Smiley
Offline davedes
« Reply #2 - Posted 2012-12-31 18: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();

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #3 - Posted 2012-12-31 18: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?

Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2012-12-31 18: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.
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


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

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

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

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


Check out my Devblog! Smiley
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #7 - Posted 2012-12-31 21: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?

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

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

Check out my Devblog! Smiley
Offline ReBirth
« Reply #9 - Posted 2013-01-01 00:48:49 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mac70
« Reply #10 - Posted 2013-01-01 01: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

I think this tutorials will help you to understand better. Wink

Check out my Devblog! Smiley
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


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

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

Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #12 - Posted 2013-01-01 17: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

Offline LunaticEdit

Senior Member


Medals: 8
Projects: 1



« Reply #13 - Posted 2013-01-01 17: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 Cheesy

Offline DrZoidberg

Senior Member


Medals: 10



« Reply #14 - Posted 2013-01-01 17: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
Offline ReBirth
« Reply #15 - Posted 2013-01-02 12: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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2013-01-02 13: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.

xsi3rr4x (35 views)
2014-04-15 18:08:23

BurntPizza (31 views)
2014-04-15 03:46:01

UprightPath (46 views)
2014-04-14 17:39:50

UprightPath (29 views)
2014-04-14 17:35:47

Porlus (46 views)
2014-04-14 15:48:38

tom_mai78101 (67 views)
2014-04-10 04:04:31

BurntPizza (127 views)
2014-04-08 23:06:04

tom_mai78101 (227 views)
2014-04-05 13:34:39

trollwarrior1 (192 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!