1

I'm currently working on a simple 2d game library for self education only. Until know everything is working as expected.

Now I'm planning the movement and the events triggered by the 'gameflow' (e.g. timer) or the entities itselves. I was wondering if it is possible to have custom events like 'EntityEventListener' and 'EntityMotionListener'. Then I had a look at MouseListener and its parent classes. Then I wrote for each listener a listener interface and an adapter class like this:

public interface AppEntityEventListener extends EventListener
{
 void onCreated(Event e);
 void onDeleted(Event e);
 void onStepped(Event e);
 void onSelected(Event e);
}
public abstract class AppEntityEventAdapter implements AppEntityEventListener
{
 @Override public void onCreated(Event e) { } 
 @Override public void onDeleted(Event e) { } 
 @Override public void onStepped(Event e) { } 
 @Override public void onSelected(Event e) { }
}

I've detected that I only can add the listeners to Components and the Entity class is not derived from a Component respectively JComponent.

I read a bit about Listeners but I don't get the point how to deal with it as I need them for now.

Considering that my questions are now:

  • Are there any bad things about these classes?
  • How can I add these listeners the best / easiest way to my Entity class?

Thanks in advance.

EDIT:

I've added all the methods like you said. So now I've got two List objects called eventListeners and motionListeners each has its own add and remove function.

I have got a further question regarding the iteration, using the following code:

private void iterateListeners()
{
 for (Object obj : eventListeners.toArray())
 {
 AppEntityEventListener l = (AppEntityEventListener) obj;
 Event e = new Event(this, Event.ACTION_EVENT, this);
 l.onCreated(e);
 l.onDeleted(e);
 l.onSelected(e);
 l.onStepped(e);
 }
 // ... other listener ...
}

How to deal the Event at this point? Is this the right way I really have no clue ... this code above is just intuitive.

asked May 5, 2012 at 11:37

2 Answers 2

1

You can add listeners to any object you like, provided the object has a method which allows adding (and removing) a listener. Just add addXxxListener()/removeXxxListener() methods to your object.

These methods should simply add/remove the listener to/from a collection of listeners, and the object should iterate through this collection and call the onXxx() method when appropriate.

answered May 5, 2012 at 11:44
8
  • One caveat to know about: when you pocess an event, the handler could call add/remove methods and then you get a ConcurrentModificationException. You must iterate over a copy of the listener list. Commented May 5, 2012 at 11:46
  • Thanks for your help. I added these add and remove methods. But I don't get the part with this list I have to interate through. Which list are you talking about? Sorry for this question but I'm new to this part in java. Commented May 5, 2012 at 12:13
  • The principle of the addListener method is to add the listener to an internal list of the object. The principle of the removeListener method is to remove the listener from this internal list. The principle of firing an event is to iterate through the list of listeners, and call their onXxx() method. There is nothing magical with listeners. Commented May 5, 2012 at 12:16
  • Ah ok I've got it. Thank you very much indeed. Commented May 5, 2012 at 12:18
  • I updated the post above because I had one further question. You may have a look at this too? Would be great, thanks. Commented May 5, 2012 at 14:10
0

See if the framework tide http://abyu.github.io/tide/ can help you. Using tide you create your own events, raise and handle those events.

answered Jan 30, 2015 at 10:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.