Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
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  
  RuntimeException error - How/Why am I getting it?  (Read 811 times)
0 Members and 1 Guest are viewing this topic.
Offline _Scyth_

Junior Member





« Posted 2013-01-23 21:30:48 »

Can anyone explain to me what a RuntimeException is?

I'm getting it from these bits of code when I try to do something when the secs get to 0 :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
 t = new Thread(){
          public void run() {
              while(game_active) {
              secs = secs - 1;
               try
               {
               Thread.sleep(1000);
               } catch (InterruptedException e) {
               e.printStackTrace();
               }
              }
             }
            };
            t.start();


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void update(){
if( secs == 0)
        {
           GameOver();
        }
}
// GameOver
    private void GameOver()
     {
       Dialog.alert("Game Over!");
     }
     


Or if i change it i get an illegalstateexcpetion, however if i use System.exit(0) it simply closes it, no errors.

Any ideas?
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #1 - Posted 2013-01-23 21:39:30 »

Can you post the stack trace (all of the error) please?

Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #2 - Posted 2013-01-23 21:46:50 »

Secs is being used / changed in separate threads. Unless you know what you're doing, you really should NOT be making additional threads. Why do you want to count seconds like this? Don't you have a deltaTime somewhere in your gameloop, which you could use to count time passed?

EDIT: Forgot an important "NOT" there...phew

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

Junior Member





« Reply #3 - Posted 2013-01-23 21:51:11 »

I'm using the BlackBerry JDE so i dont have deltaTime so i need to count in this way, I tried using a Timer and TimerTask but that was given me errors as well.

It tells me there is no stack trace.
It just repeats saying, NO STACK TRACE

how i can i get the same result using the two threads?
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #4 - Posted 2013-01-23 21:53:45 »

how i can i get the same result using the two threads?

Two threads wont cause exceptions like this, he's pointing out that multiple threads handling the same non-volatile or synchronized variable will have problems. One could update the value the same time as the other, and you could have funky effects.

Why are you using multiple threads?

Offline GabrielBailey74
« Reply #5 - Posted 2013-01-23 21:59:45 »

You could use this timer instead of a thread:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class SimpleTimer {
   private long cachedTime;

   public SimpleTimer() {
      reset();
   }

   public void reset() {
      cachedTime = System.currentTimeMillis();
   }

   public long getElapsed() {
      return System.currentTimeMillis() - cachedTime;
   }
}


Example to keep track of seconds:
1  
2  
3  
4  
5  
6  
7  
8  
9  
/* Before you inititialize your first thread */
final SimpleTimer secondCounter = new SimpleTimer();

/* In your run method: */
if (secondTimer.getElapsed() >= 1000) {
   seconds -= 1;
   /* Timer has reached one second, reset the timer */
   secondTimer.reset();
}

There's no need to use a thread for a task so simple, use a simple timer! Grin
lol, hope it helps mate.

Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #6 - Posted 2013-01-23 22:01:09 »

A very good solution Smiley

- Jonas
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #7 - Posted 2013-01-23 22:02:50 »

Yeah, that solution to keep track of time is sufficient. That crazy thread stuff you're doing is a bad idea mmmkay?

Offline _Scyth_

Junior Member





« Reply #8 - Posted 2013-01-23 22:27:11 »

Thanks, i didnt know it would be that easy Smiley
Offline Varkas
« Reply #9 - Posted 2013-01-23 22:59:49 »

Can anyone explain to me what a RuntimeException is?

Simple: Your program runs timely into an exceptionally problematic section of code.

Sorry, couldn't resist  Grin


if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Scyth_

Junior Member





« Reply #10 - Posted 2013-01-24 18:28:37 »

Well done Tongue
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 (80 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

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