Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Reducing Lag in java 2D game  (Read 1447 times)
0 Members and 1 Guest are viewing this topic.
Offline Shane75776
« Posted 2010-04-13 00:52:45 »

Ok so I have been making this game for a while now and adding in features here and there and just recently i decided to create my own type of particle effect for explosions. Ever since I did this my game starts to get pretty bad lag in the higher levels (6-7+).

I have tried lowering the amount of particles as much as I can to still have it look like an effect and I still get lag.
I do have about 5 for loops that each activate to create each particle needed depending on what is being blown up, is it possible having 5 for loops is causing this? 

The only other thing I could think of is that my Board class has a ton of code that I should be putting into new classes, around 2100 lines of code where about half or more of that could be split up into different class files. I just have been to lazy to do it. Could this cause lag?

Here is a link to my game if you want to try it. Its a .msi download because I made it installable using Advanced Installed because I have no idea on how to use or set up a JLP and most tutorials are useless or confusing.

Here is the link to it.
http://shaneisrael.homelinux.com:1337

The game is called Galaxy Defender.


EDIT:
I am using a Timer to continuously run the actionPerformed and Paint method. My board is also extending a JPanel.

I Believe I know the issue. I think its my thread. I believe its doing this because after each level it stops the thread with thread.stop; and then when i begin a new level it creates the thread with the following method.
1  
2  
3  
4  
5  
6  
7  
   private void initGameThread()
   {
      // TODO Auto-generated method stub
     gThread = new GameThread(this, craft);
      gThread.start();
      GameThread.ALIEN_SHOT_DELAY -= 5;
   }


Is there a better way to do this so that I do not have to keep stopping the thread and recreating it? Also I think it may be because within my thread to keep it running I am constantly calling the run(); method from within itself "recursion" and I believe its acting like a while loop and using up my system resources fairly quick. Is there a way around this?

Here is my current game thread 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  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.Random;

import javax.swing.Box;


public class GameThread extends Thread
{
   static int DELAY = 100;
    Board b;
    Craft c;
    Random r = new Random();
    float bs, bd, hp, os, sw,ts,ds, reload;
    static int shotTimer = 0;
    int bsI, bdI, hpI, osI, swI, tsI,dsI, fireTimer = 0;
    int[] am = new int[1000], ranTimeI = new int[1000];
    double[] ranTime = new double[1000];
    public static int ALIEN_SHOT_DELAY = 105;
    public static int ALIEN_SHOT_DELAY_MIN = 10;
   
    static double reloadDelay;
   
    public GameThread(Board b, Craft c)
    {
       bsI =r.nextInt(100)+150;
       bdI =r.nextInt(100)+100;
       hpI = r.nextInt(100)+200;
       osI = r.nextInt(100)+300;
       swI =r.nextInt(100)+150;
       tsI = r.nextInt(100)+200;
       dsI = r.nextInt(100)+100;
//       tsI = 5;
//       dsI = 10;
     
       this.b = b;
       this.c = c;
       GetDelayBetweenShots();

    }

   private void GetDelayBetweenShots()
   {
      // TODO Auto-generated method stub
     
     
       if(b.LEVEL > 1)
       {
          for(int i = 0; i < b.alienList.size(); i++)
          {
             
             if(b.boss)
             {
                ranTimeI[i] = 10;
             }
             else
             {
                ranTimeI[i] = r.nextInt(ALIEN_SHOT_DELAY)+ALIEN_SHOT_DELAY_MIN;
             }
          }
       }
     
   }

   public void run()
    {
     
        try
        {
           Thread.yield();
           Thread.sleep( DELAY );
           bs+=1; bd+=1; hp+=1; os+=1; sw+=1; ts+=1;ds+=1; shotTimer+=1; fireTimer+=1;
           if(b.LEVEL > 1)
           {
              for( int i = 0; i < b.alienList.size(); i++)
              {
     
                 ranTime[i]+=1;
              }
           }
             

        }
        catch(Exception e)
        {
            System.out.println( "An error occured in " + e.getMessage());
        }
//Will make this drop the trip shot power up then when its grabbed it will make c.tripShot = true
//and will set the shotTimer to 0 this will take place in the collision section.
       if(ts == tsI)
        {
           b.TrippleShot();
        }
        if(ds == dsI)
        {
           b.DoubleShot();
        }
        if(shotTimer == 120)
        {
           if(c.tripShot)
              ts = 0;
           else if(c.doubleShot)
              ds = 0;
           
           c.tripShot = false;
           c.doubleShot = false;
           
        }

       if(bs == bsI)
       {
          b.Bigshot();
          bs = 0;
       }
         if(bd == bdI)
       {
          b.Bubble();
          bd = 0;
       }
         if(hp == hpI)
       {
          b.Health();
          hp = 0;
       }
         if(os == osI)
       {
          b.Overshield();
          os = 0;
       }
         if(sw == swI)
       {
          b.PowerUp();
          sw = 0;
       }
         if(b.LEVEL > 1)
         {
            for(int i = 0; i < b.alienList.size(); i++)
            {
               Alien a = (Alien) b.alienList.get(i);
               if(ranTime[i] == ranTimeI[i])
               {
                  try
               {
                  a.fire();
               }
                  catch (IOException e)
               {
                  // TODO Auto-generated catch block
                 e.printStackTrace();
               }
                  System.out.println(ranTimeI[i]);
                  ranTime[i] = 0;
               }
            }
         }
         CheckFireTimer();
         run();
    }

   private void CheckFireTimer()
   {
      // TODO Auto-generated method stub
     if(fireTimer >= 2)
      {
         if(b.fire)
           {
            if(!b.pause)
            {
               fireTimer = 0;
               try
               {
                  b.playCraftAudio();
               }
               catch (IOException e1)
               {
                  // TODO Auto-generated catch block
                 e1.printStackTrace();
               }
               Craft.fire();
               Board.shotsFired+=2;
            }
           
           }
      }
     
   }
}

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2010-04-13 12:29:01 »

This is too broad a problem for any hope of a forum member to invest time an effort in narrowing the problem for you. I suggest you profile your game to give you actual statistics which should help to narrow the problem.
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #2 - Posted 2010-04-13 12:50:01 »

yeah, I didnt really go through much of your code. but I did notice "Thread.sleep( DELAY );"

this is not a good way to handle pauses, as it doesnt take into account the previous calculations that have taken place since the last sleep() call.

Here is a Timer class that works really well for keeping a constant time, in pure java (also applet friendly)

it has a main() method to show you how to use it. instead of sleep() call timer.sync();
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  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
public class BasicTimer {
   private int fps;
   private long timeThen;
   boolean newVersion = true;
   public BasicTimer(int frameRate) {
      if (System.getProperty("java.version").startsWith("1.4"))
         newVersion = false;
      if (newVersion) {
         fps = frameRate;
         timeThen = System.nanoTime();
      }
      else {
         fps = frameRate;
         System.out.println("Old Version Detected:" +
            Running Old Java Timer Version");
         timeThen = System.currentTimeMillis();
      }
   }
   public void changeFPS(int frameRate) {
      fps = frameRate;
   }
   public void sync() {
      if (newVersion) {
         long gapTo = 1000000000L / fps + timeThen;
         long timeNow = System.nanoTime();
           
         while (gapTo > timeNow) {
            try { Thread.sleep(1);
            } catch (InterruptedException e) {}
            timeNow = System.nanoTime();
         }
         
         timeThen = timeNow;
      } else {
         long gapTo = 1000 / fps + timeThen;
         long timeNow = System.currentTimeMillis();
           
         while (gapTo > timeNow) {
            try { Thread.sleep(1);
            } catch (InterruptedException e) {}
            timeNow = System.currentTimeMillis();
         }
         
         timeThen = timeNow;
      }
   }
   
   
   public static void main(String args[]) {
      System.out.println("
Simple Timer Test: " +
         "
set to 2 (should actually be 60 for most games)");
      BasicTimer timer = new BasicTimer(2);
      while (true) {
         timer.sync();
         System.out.println("
TICK");
      }
     
   }
}

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jojoh

JGO Knight


Medals: 5
Projects: 7


games4j.com


« Reply #3 - Posted 2010-04-13 13:44:29 »

If it really is a problem of timing in the main loop then you can read a bit about it here: Timing in main loops
@ Bobjob: Looks like your code would fit well in at "Call for help: Code example" under "Fixed framerate" on that page. Do add the code if it makes sense.

@ Shane75776, do as Moogie suggests, and profile the code in any case. You could spend lots of time fixing the wrong thing, if you don't really know where the problem is at.

Offline CommanderKeith
« Reply #4 - Posted 2010-04-13 15:15:11 »

1  
2  
3  
4  
5  
while (gapTo > timeNow) {
            try { Thread.sleep(1);
            } catch (InterruptedException e) {}
            timeNow = System.currentTimeMillis();
         }


I've had trouble with such methods because the sleep(x) never sleeps for that amount, usually it sleeps for 20ms for any x < 20, so there's no accuracy (at least on windows)

I've seen this type of thing used before though:

1  
2  
3  
4  
         while (gapTo > timeNow) {
            Thread.yield();
            timeNow = System.currentTimeMillis();
         }


but I haven't used it yet... i normally do non-fixed time steps.

Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2010-04-13 17:41:24 »

You probably don't want to use Thread.stop(), this method is deprecated and is very unstable.

Also, you shouldn't invoke run() at the end of the run() method to create loops.  Eventually you'll reach the stack limit and crash (and the only reason you're not is that you have a 100ms delay each time probably).  I would organize your run() like so:
1  
2  
3  
4  
5  
public void run() {
   while (playGame) {
       // do everything from old run() except for the last call to run
  }
}


You'll need to define a member variable "playGame" and make sure it's declared volatile:
1  
volatile boolean playGame

This lets the Threads see changes right away.  Then when you need to stop the thread, just set playGame to false and it will end gracefully.

Offline Shane75776
« Reply #6 - Posted 2010-04-14 03:10:04 »

Thanks for the replies, It has helped alot. And sorry about making it such a broad statement I didnt post all the code because it isnt very clean and wold be confusing as hell to go through if you are not me, So thats why I only posted that little bit.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
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.

CogWheelz (12 views)
2014-08-01 22:53:16

CogWheelz (14 views)
2014-08-01 22:51:43

CopyableCougar4 (15 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!