This site requires JavaScript, please enable it in your browser!
Greenfoot back
Super_Hippo
Super_Hippo wrote ...

2013/11/29

Bomberman: Problem with changing a variable in an other specified object from another class

Super_Hippo Super_Hippo

2013/11/29

#
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
//kinder [German] = children [English]
    private int kindertimer = 0;
    private int kinder = 0;
public int kinder = 0; //kinder [German] = children [English]
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
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
        }
    }
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
    public static int kindertimer = 0;
    public static int kinder = 0;  
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
Super_Hippo Super_Hippo

2013/11/29

#
It would probably work if I would create many same classes with only different names Marienkaefer1 Marienkaefer2 Marienkaefer3 ... and Marienkaeferkind1 Marienkaeferkind2 Marienkaeferkind3 ... but I want to know if it is possible with just one class for each object type.
danpost danpost

2013/11/29

#
The easiest way to control the child count of the ladybugs is to use a private non-static int field in the ladybug class to track the number of children. The key for controlling the number of children for each ladybug lies with the children themselves. If each child 'knew' who its parent was, then they could inform the parent when it dies. When creating the children (from the Marienkaefer class), pass 'this' (a reference to the parent) to the child:
Marienkaeferkind kind = new Marienkaeferkind(this);
and save the parent in the class of the child:
// instance field
private Marienkaefer muder;
// constructor
public Marienkaeferkind(Marienkaefer mdr)
{
    muder = mdr; // saves parent in instance field
}
Then when dies (still in Marienkaeferkind class):
getWorld().removeObject(this);
muder.loseKind();
and add the method in the Marienkaefer class:
public void loseKind()
{
    kinder--;
    kindertimer = 0;
}
Forget the IDing; forget 'static' fields. EDIT: forget multiple classing.
Super_Hippo Super_Hippo

2013/11/29

#
Thank you very much! It works as it should. I am happy that I asked because I probably would have never find this out by myself. I still have a few questions: I thought that I always have to use something like "int" or "boolean" where you just put the name "Marienkaefer muder". How does this exactly work? And what does the word "muder" mean? Is it just a bunch of random letters or does it have a special reason?
danpost danpost

2013/11/29

#
'muder' should have been spelt 'mutter' (for Mother). 'Marienkaefer muder' is used just like you use 'int value' or 'boolean state'. The first part declares what the second part holds. 'value' will hold an 'int' value; 'state' will hold a 'boolean' (true/false) state; and 'muder' ('mutter') will hold a 'Marienkaefer' object. The only difference is that 'int' and 'boolean' are primitive (basic) types that are already known by Java. All other field types are of Object type (or a subclass of the Object class -- including String; however, 'String' has special recognition within Java and does not need to be imported or added to your projects -- it is treated like a 'primitive' type).
Super_Hippo Super_Hippo

2013/11/30

#
Ok, I did not get this wrong spelled German, sorry. So I can use the name of another class instead of these other types. Sounds very interesting. I just updated my game. Thank you again. :)
You need to login to post a reply.