Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: EventBus on: 2011-12-31 22:28:26
So I have been looking at this and one way in which it could be done was to use a "ConcurrentSkipListSet<E>" instead if a list which if I understand your point Riven would be solved as there is no longer a list that can throw the problem. Another idea I found was using annotations to signify a event handler this removes the need for Anonymous Inner Classes which apparently the GC and has caused memory leaks and so long as I ensure i keep track of the method and not keep looking up shouldn't slow anything down.

Also both the set and use of reflection with annotations is supported in Andriod and actually suggested.
2  Game Development / Newbie & Debugging Questions / Re: EventBus on: 2011-12-31 21:30:32
Yes true but in most cases you would register an event long before your actually gonna fire any... since object creation need be avoided in the update loop.

p.s Thanks for the quick reply as always riven Smiley

EDIT:
although thinking about it firing an event involves creating one in the first place, hmm.

Also does it mean it worth doing it this way (after i solve the concurrency etc out)?
3  Game Development / Newbie & Debugging Questions / EventBus on: 2011-12-31 21:15:28
Hey guys me again,
  I have been having some issues with maintaining my slick2D game in regards to notifying other areas of code something has happened. My solution was to create EventBus essentially a very simple version of the GWT version (I work with it at work so to me its second nature).

The code looks as follows:
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  
package uk.co.digitaldragonstudios.engine.events;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventBus {
   private static EventBus eventBus;
   private Map<EventType,List<Listener<Event>>> bus =
      new HashMap<EventType,List<Listener<Event>>>();
   
   public static EventBus get() {
      if(eventBus == null) {
         eventBus = new EventBus();
      }
      return eventBus;
   }
   
   public void addListener(EventType type,Listener<Event> event) {
      if(bus.get(type) != null) {
         bus.get(type).add(event);
      }
      else {
         List<Listener<Event>> listeners = new ArrayList<Listener<Event>>();
         listeners.add(event);
         
         bus.put(type, listeners);
      }
   }
   
   public boolean fireEvent(EventType type,Event event) {
      if(bus.get(type) != null) {
         List<Listener<Event>> listeners = bus.get(type);
         for(Listener<Event> listener : listeners) {
            listener.onEvent(event);
           
            if(event.isCancelled()) {
               return false;
            }
         }
         return true;
      }
      return false;
   }
}


My questions are, is there a better way to do it than this and should it be done in games?

i understand it would never work for Java 4k of course Wink
4  Game Development / Newbie & Debugging Questions / Re: Creating many entities of an enemy or object from one method on: 2011-12-30 21:01:02


1  
2  
3  
4  
5  
6  
7  
public void setX(int x) {
  this.x = x;
}

public void setY(int y) {
  this.y = y;
}
5  Game Development / Newbie & Debugging Questions / Re: Creating many entities of an enemy or object from one method on: 2011-12-30 20:10:21
so long as the field is inside the Boulder class and belongs to the boulder it can be anything you want.

a List<Boulder> is simply a collection of Boulder instances all have the same class so anything you could do with:

1  
2  
3  
4  
boulder1 = new .Boulder(this);
boulder1.explode();
boulder2 = new Boulder(this);
boulder2.explode();


you can do with a List by looping through it

1  
2  
3  
for(Boulder b : boulders) {
   b.explode();
}
6  Game Development / Newbie & Debugging Questions / Re: Creating many entities of an enemy or object from one method on: 2011-12-30 19:57:06
That was a very simplified version of it i'll admit but take for example update();

So if you created the boulders with different positions
1  
2  
3  
4  
5  
6  
for(int i = 0; i < 3; i++) {
    Boulder b = new Boulder(this);
    b.setX(i*10);
    b.setY(i*10);
    boulders.add();
}


Above would put them all in a diagonal line across your screen

Then for update Boulder would contain something like:

1  
2  
3  
4  
public void update() {
   x += 1;
   y += -1;
}


of course in real world you would calculate velocity of the boulders instead of just move them across the screen.

Then to draw you would loop through them all like before and call draw:
1  
2  
3  
public void draw(Graphics g) {
   g.drawImage(image,x,y,width,height);
}


Oh by update and draw i mean the update and draw in your game loop
7  Game Development / Newbie & Debugging Questions / Re: Creating many entities of an enemy or object from one method on: 2011-12-30 19:23:39
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
List<Boulder> boulders = new ArrayList<Boulder>();

//Create some boulders
for(int i = 0; i < 3; i++) {
    boulders.add(new Boulder(this);
}

//Update
for(Boulder b : boulders) {
  b.update();
}

//Draw
for(Boulder b : boulders) {
   b.draw();
}


Others improvements would be a base class all you entities inherit from. have delta in your for smooth movement. Hope this helps
8  Game Development / Newbie & Debugging Questions / Re: About: Slick2D and easy-to understand Code on: 2011-12-26 16:42:23
I went looking over the forums and they are a nice bunch they allow you to download their games source. one particular game Called Crush taught me loads so thanks goes to Lex Cheesy, I'm now currently moving an old game to slick for comparison going well so far Smiley
9  Game Development / Newbie & Debugging Questions / Re: Centering text in a JFrame on: 2011-12-25 03:30:58
I've always done it by (for games at least)
1  
2  
FontMetrics metrics = graphics.getFontMetrics(yourFont);
Rectangle rect = metrics.getStringBounds(str, graphics);


then using the JPanel size and performing a quick bit of math Smiley
10  Game Development / Newbie & Debugging Questions / Re: Slick2D Question on: 2011-12-25 01:37:27
should be able to turn it off with

1  
2  
3  
4  
5  
public static void main(String[] argv) throws SlickException {
      AppGameContainer container = new AppGameContainer(new Game(), 640, 480, false);
      container.setShowFPS(false);
      container.start();
   }


By default its set to true
11  Game Development / Newbie & Debugging Questions / Re: Yet another game loop >< on: 2011-12-24 19:25:14
Even the:
1  
System.nanoTime()
, I thought it was released by Java for the very reason of certain OS's not having a high enough granularity?
12  Game Development / Newbie & Debugging Questions / Re: Yet another game loop >< on: 2011-12-24 01:44:21
Hmm i see your point this all stems from an issue i have where my paddle on a simple block breaker clone is freezes for a split second yet animation continues even updates continue as the balls carry on moving
(quite annoying since I now cant win Tongue ). All I can guess is it something im missing when it comes to keyboard inputs.
13  Game Development / Newbie & Debugging Questions / Re: Yet another game loop >< on: 2011-12-24 00:52:18
Well i work with java as a job just not gaming and i found it always a good idea to follow "best practises" to keep the code easy to maintain and test. I guess i am trying to find the best approach to making the game loop and the game in general. When it comes to game programming i am a newbie. I will try to explain what i trying to do here and maybe you nice people could be so kind to help Smiley

I want a general architecture to handle everything about the game from resource management to the famous game loop, ability to set full screen on a game would be a nice option too. I understand there is plenty of libraries that will handle all of this for me but i'm afraid i wont learn which could cause more problems in the future Smiley
14  Game Development / Newbie & Debugging Questions / Re: Yet another game loop >< on: 2011-12-23 21:15:40
Thanks  Grin, I will give that a go
15  Game Development / Newbie & Debugging Questions / Yet another game loop >< on: 2011-12-23 20:47:09
Hey guys i'm new here.
I have been working on a Block breaker clone for desktop as a project to get me used to the best practices and designs idioms for Java games. I have a first version which worked great on my more powerful laptop but when I went to my Net book I found it stuttered badly.I have seen all your different game loops but with no definitive answer on the best I was wondering if you guys could suggest improvements for my attempt at it Smiley

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  
private void gameLoop() {
      // Regenerate the game objects for a new game
     // ......
     state = State.PLAYING;
   
      // Game loop
     long beginTime, timeTaken, timeLeft;
      while (true) {
         beginTime = System.nanoTime();
         if (state == State.GAMEOVER) break;  // break the loop to finish the current play
        if (state == State.PLAYING) {
            // Update the state and position of all the game objects,
           // detect collisions and provide responses.
           gameUpdate();
         }
         // Refresh the display
        repaint();
         // Delay timer to provide the necessary delay to meet the target rate
        timeTaken = System.nanoTime() - beginTime;
         timeLeft = (UPDATE_PERIOD - timeTaken) / 1000000L;  // in milliseconds
        if (timeLeft < 10) timeLeft = 10;   // set a minimum
        try {
            // Provides the necessary delay and also yields control so that other thread can do work.
           Thread.sleep(timeLeft);
         } catch (InterruptedException ex) { }
      }
   }


As a side question is it better to leave Swing to handle the screen or should I handle that myself?

Thanks guys
AbtractChaos
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (24 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!