1

I am new to Android and Java development and am looking to implement a custom event, however having done lots of research and trying various different approaches i cannot seem to get it to work.

I am essentially trying to create a battle system, which i have implemented in .NET but cannot seem to get working in Java, it requires me to create events for things such as DealtDamage, DamageReceived, Died etc.

This will be inherteing information from the characters object which consits of thing such as hp, attack, defence etc.

Can someone please provide sample code how I can create these events and then action them in various classes.

e.g. Damage Dealt

user1 atk - user 2 def = totalatk return totalatk

Thanks

SingleWave Games
  • 2,618
  • 9
  • 36
  • 52
  • If you've already done it in .net, can you use MonoTouch? – i_am_jorf Sep 17 '12 at 16:40
  • 1
    Also this question is unclear, is this just on device or over network to server side? – Morrison Chang Sep 17 '12 at 16:41
  • 1
    user1 atk - user 2 def = totalatk return totalatk ?????? – Simon Sep 17 '12 at 16:43
  • @MorrisonChang it is all on the device, I get all my data from an SQLite datbase. – SingleWave Games Sep 18 '12 at 09:05
  • @Simon, as i am creating a battle system take for example you character is atacking a monster. Your characters attack points are 10, however you monster has a defense of 5 points. Therefore, when you attack the monster you will only actually cause 5 points of damage. Hope that is clearer. Thanks – SingleWave Games Sep 18 '12 at 09:06
  • @jeffamaphone, I have already created a big chunk of my game and its only that small section which I had done in .NET, therefore MonoTouch is not an option. Thanks anyways – SingleWave Games Sep 18 '12 at 09:43

1 Answers1

2

If you are trying to generate callbacks from your class (raise events):

In the class which generates the callback:

public interface EventHappened{
        void callback(int arg1, String arg2);
}

...

ArrayList<EventHappened> eventHappenedObservers = new ArrayList<~>;

...

public void setEventHappenedObserver(EventHappened observer){
     eventHappenedObservers.add(observer);
}

...

if (eventHasHappened){
    for (EventHappened eventHappenedObserver:eventHappenedObservers){
        eventHappendedObserver.callback(event.number,event.toString());
    }
}

...

In the consuming class:

instanceOfClassRaisingCallback.setEventHappendedObserver(new EventHappened{
     @Override
     void callBack(int arg1, String arg2){
          doStuffWithArgs(arg1,arg2);
     }
   )};

(from memory, apologies for typos and syntax errors but you get the idea...I hope)

Good luck.

Simon
  • 14,407
  • 8
  • 46
  • 61