Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (428)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  animation / timing issue  (Read 1095 times)
0 Members and 1 Guest are viewing this topic.
Offline maxim

Junior Newbie





« Posted 2009-03-04 21:09:45 »

Hi guys first post.

ill try and explain this with pseudo code but will recreate the problem in actual code if i cant explain it very well.

this is just for a simple game im making.

baiscly i want this to happen
  • user clicks on the screen.
  • the screen then updates.
  • waits one second
  • then updates the screen again.

i think what  happens when i implement this is the repaint requests get combined on the event dispatch thread and dont update the way it i thought when i wrote the code. but im not sure.

let me try some pseudo code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
//class members
boolean background = false;
boolean square = false

paintScreen() {

  if (background == true) {
     paint the screen blue
  } else {
     paint the screen green
  }

   is (square == true) {
   paint a square
   }


}

//when users clicks on screen run this function

clickOnScreenEvent() {

  square = !square;

  paintScreen();

  pauseProgram('1 second');

  background = !background;

  paintScreen();

  }

}


that's basically what my program dose

the logic to me seems fine but it doesn't work. basically you never see the square get painted. it just changes the background.

also i use Thread.sleep(1000); which im pretty sure locks up the EDT so yeah how would i go about implementing this in java ?


Offline Wildern

Junior Member





« Reply #1 - Posted 2009-03-04 21:46:26 »

You could change your variables from booleans to longs and then, where you set them to true, call System.nanoTime().
In you check for true, you would get the current time with another call to System.nanoTime() and then subtract the two to get your current elapsed time.  If it is still less then your threshold (one second) then your condition is true, otherwise, it is false.
No sleep to mess up the EDT.
Offline maxim

Junior Newbie





« Reply #2 - Posted 2009-03-04 21:55:08 »

i don't think that will work i my game. there is no game loop.

in my game the screen actually only needs to repaint when the user clicks on the game screen or a button.

i could write a game loop easy tho. which would enable me to upgrade the game with animations but yeah i dunno. is that the correct way to go about it ? with the system time thing ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline maxim

Junior Newbie





« Reply #3 - Posted 2009-03-04 22:53:15 »

ok i found a dead simple way around the issue.


i made a very basic game loop.

basically then i do this

  • click mouse on screen (change a boolean flag)
  • force repaint by calling paintImmediately
  • pause the gameLoop for 1 second(which repaints the screen)
  • change another boolean flag which causes diffrent things to be painted
  • gameloop now resumes updating the screen as normal

pretty sure this is kind of a simple work around that will not scale well if i want to add animations.
which i do want to do at some future stage. im happy for now but i think ill have to re write the whole game so it scales better. right now its dead simple. Its my first game.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
if (playerOneTurn == true) {

                     paintImmediately(0, 0, getWidth(), getHeight());

                    try {
                        gameLoop.sleep(2000);  // pause for 2 seconds
                   } catch (InterruptedException ex) {
                    }

                    //this like filps the boolean to its opposite value in this case false so it will draw diffrent
                   //background to the screeen once the gameloop comes out of its 2 second sleep

                    playerOneTurn = !playerOneTurn;
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 (79 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (221 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!