Hello,
for a few days, I am working on my bomberman game again: Link
I want my new version to have more levels than the previous one (5), so I want to make it more complex. In the fifth level of the game, there is a ladybug as a monster which creates smaller ladybugs. These ladybugs are her "children". The maximum is three. So if one of the small ladybugs die, the main ladybug can create another one.
I store the current number of children in the 'Marienkaefer' class.
code1
If the ladybug gets a child, these variable increases and if this number is at three, she can not create more of them.
In the class 'Marienkaeferkind' I had this method. If this ladybug has contact with class 'Feuer' , then it should die and be removed from the screen. In addition, the variable 'kinder' in the class 'Marienkaefer' should decrease, so this ladybug can create another child again.
code2
This gave me the error that 'kindertimer' and 'kinder' have private access in class 'Marienkaefer'. So I changed the "private" in code1 (see above) to "public". Then it says that the "non-static variable kindertimer cannot be referenced from a static context". Well, I added a static then:
code3
It did work and probably I could do that otherwise without static variables somehow (although I am not sure how).
But here starts the problem! What do I do when there is more than one ladybug on the screen which can create children? The static variables are stored to all objects of this class if I got that right. (Although that was my personal definition of 'public'.) That means that it does not work like this. So how can I make sure that every ladybug can get three children and recreate them again if one of them die?
I thought of giving a created ladybug an ID (random number between 0 and 1000 or something). Then I can give that ID to its children to know which children relates to which ladybug. But I have no idea how to change the variable in the right ladybug class then if one of its children die.
I hope you can understand what I mean. If not, please ask.
Thank you for you help in advance!
Hippo
//kinder [German] = children [English] private int kindertimer = 0; private int kinder = 0;
public int kinder = 0; //kinder [German] = children [English]
public void feuer() { if (canSee(Feuer.class) ) { getWorld().addObject(new Totenkopf(), getX(), getY() ); //skull and crossbones Spieler.gegner--; //number of enemies on the screen. Important to know when to open/close the exit door. Marienkaefer.kindertimer = 0; //timer to make sure that the ladybug does not create all children at once and not immediately when it could Marienkaefer.kinder--; //number of children of the ladybug getWorld().removeObject(this); //simply die } }
public static int kindertimer = 0; public static int kinder = 0;