Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  need to be able to wait inbetween image changes.  (Read 787 times)
0 Members and 1 Guest are viewing this topic.
Offline _Scyth_

Junior Devvie





« Posted 2013-06-09 20:06:42 »

ok, so I have a slot machine game, and whats a slot machine game, if the reels dont spin before changing to the final images.

so what i want to do is:
draw image
wait 100 milliseconds
draw image
wait 100 milliseconds

and so on,

now i have tried
1  
2  
3  
4  
5  
6  
7  
Slot1.setImage(image1);
invalidate(); // <-  the repaint method
Thread.sleep(100);
slot1.setImage(image2);
invalidate();
Thread.sleep(100);
slot2.setImage(image3);


but all it does is wait the 200 milliseconds then changes it to the final image, without showing the image1 or image2.

now i dont have a game loop, because, i dont actually need one with this game, as the screen only needs to be rendered when one button is clicked, not so many times a second.

so what i would like to know, is how can i get this to wait?
Offline Longarmx
« Reply #1 - Posted 2013-06-09 20:25:23 »

Maybe because you are setting the images to slot1 the first two times then setting the final image to slot2...

Offline davedes
« Reply #2 - Posted 2013-06-09 20:32:11 »

You need a timer of some sort. Usually this is done with a game loop but if you aren't using one, you could use java.swing.Timer.

1  
2  
3  
4  
5  
6  
7  
8  
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //...Perform a task...
            }
        };
        Timer timer = new Timer(1000, taskPerformer); //1 second
        timer.setRepeats(false);
        timer.start();


This should cover all the Swing threading stuff for you, so you don't need to invokeLater on the EDT.

Just for fun, here is how you might achieve this in LibGDX:

1  
2  
3  
4  
5  
6  
7  
float delay = 1.0f; //delay in seconds

Timer.schedule(new Task(){
    public void run() {
        // Do your work
    }
}, delay);

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dermetfan
« Reply #3 - Posted 2013-06-09 20:35:24 »

You could also use incredibly unprofessional loop stuff:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
slot1.setImage(image1);
invalidate();
int duration = 200;
long time = System.currentTimeMillis();
boolean done = false;
while(System.currentTimeMillis() - time < duration) {
    if(System.currentTimeMillis() - time > 100 && !done) {
        slot2.setImage(image2);
        invalidate();
        done = true;
    }
}
slot2.setImage(image2);
invalidate();

Offline Delta517

Senior Devvie


Medals: 2
Projects: 1



« Reply #4 - Posted 2013-06-09 21:33:31 »

You could also use incredibly unprofessional loop stuff:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
slot1.setImage(image1);
invalidate();
int duration = 200;
long time = System.currentTimeMillis();
boolean done = false;
while(System.currentTimeMillis() - time < duration) {
    if(System.currentTimeMillis() - time > 100 && !done) {
        slot2.setImage(image2);
        invalidate();
        done = true;
    }
}
slot2.setImage(image2);
invalidate();


Hey! :O Don't mock me and my "professional" coding skills! Sad
Offline dermetfan
« Reply #5 - Posted 2013-06-09 22:00:43 »

You could also use incredibly unprofessional loop stuff:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
slot1.setImage(image1);
invalidate();
int duration = 200;
long time = System.currentTimeMillis();
boolean done = false;
while(System.currentTimeMillis() - time < duration) {
    if(System.currentTimeMillis() - time > 100 && !done) {
        slot2.setImage(image2);
        invalidate();
        done = true;
    }
}
slot2.setImage(image2);
invalidate();


Hey! :O Don't mock me and my "professional" coding skills! Sad

I put the "incredibly unprofessional" there because this was my first idea, and when I finished writing it the others came up with actually good ideas like davedes with the Timers... So it was more directed at me Tongue

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2013-06-10 06:34:30 »

davedes's answer is the only correct one in this thread. Smiley

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #7 - Posted 2013-06-10 06:45:51 »

I use "incredibly unprofessional" loop stuff myself Tongue

Offline _Scyth_

Junior Devvie





« Reply #8 - Posted 2013-06-10 06:55:51 »

Problem I have with the swing timer, is that I don't know if I can use the javax.swing.*; library for this game, as it is not going to be run on a computer, but a mobile device.

I tried to create a simple timer, that has a method getElapsed(); which tells me how many milliseconds have passed. The problem is I don't know how to incorporate it to add a delay, which is why the "incredibly unprofessional" loop stuff might be my only way of doing it,

And the slot2.setImage() was a type, it should have been slot1, but it doesn't matter, same concept.
Offline dermetfan
« Reply #9 - Posted 2013-06-10 09:51:11 »

You don't have to use the javax.swing.* Timer. There's also a java.util.Timer, have a look at this one, it should be available on mobile devices I guess. It's fairly simple to use:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
slot1.setImage(image1);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
   
   @Override
   public void run () {
      slot1.setImage(image2);
   }
}, 100);
timer.schedule(new TimerTask() {
   
   @Override
   public void run () {
      slot1.setImage(image3);
   }
}, 200);


davedes's answer is the only correct one in this thread. Smiley
Come on, my crappiness works as well  Sad

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

Junior Devvie





« Reply #10 - Posted 2013-06-10 15:05:36 »


davedes's answer is the only correct one in this thread. Smiley
Come on, my crappiness works as well  Sad

Lol, I'll try this when my computer boots up, thanks everyone, helps me out a lot!
Offline _Scyth_

Junior Devvie





« Reply #11 - Posted 2013-06-10 21:26:16 »

The TimerTasks took a bit of modifying, but they worked a treat! Thanks Smiley
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.

trollwarrior1 (28 views)
2014-11-22 12:13:56

xFryIx (70 views)
2014-11-13 12:34:49

digdugdiggy (49 views)
2014-11-12 21:11:50

digdugdiggy (43 views)
2014-11-12 21:10:15

digdugdiggy (37 views)
2014-11-12 21:09:33

kovacsa (61 views)
2014-11-07 19:57:14

TehJavaDev (65 views)
2014-11-03 22:04:50

BurntPizza (63 views)
2014-11-03 18:54:52

moogie (79 views)
2014-11-03 06:22:04

CopyableCougar4 (78 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!