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

2018/6/17

How to stop a single method from another class

grnft grnft

2018/6/17

#
My object touches another and a method from another class stops. Let's say the mother searches her child. When she finally finds her (and touches her), the serial killer can't kill the child anymore because of the protection of her mother. So how can I stop the method killChild() of the class killer? The game has to go on, because the mothers' protection only lasts for a certain period of time, so ending the scenario won't be a solution. Thanks in advance!!
danpost danpost

2018/6/17

#
You can regulate the calling of the killChild method depending on the protected state of the child:
if (isTouching(Child.class) && !((Child)getIntersectingObject(Child.class)).isProtected()) killChild();
This says if touching a child and that child is not protected, then kill child. You will add a method in the Child class to have it return its protected state:
public booleean isProtected()
{
    return protekted;
}
where protekted contains its protected state (that is, the conditions required for the child to be considered protected). I used protekted because protected is a java keyword that cannot be used for a user-declared variable.
grnft grnft

2018/7/2

#
It doesn't work that way. When my object hits the enemy, there only comes an error message which indicates that there's something wrong with the killChild method.
danpost danpost

2018/7/2

#
grnft wrote...
It doesn't work that way. When my object hits the enemy, there only comes an error message which indicates that there's something wrong with the killChild method.
Show what you tried and copy/paste the error message.
grnft grnft

2018/7/2

#
There's the code of my main character that should be protected:
    public void tarnen()
    {
        Voldemort voldemort;
        if(isTouching (Tarnumhang.class))
        {
            removeTouching(Tarnumhang.class);
            Timer = Timer - 1;
            if(Timer >= 0)
            {
                getImage().setTransparency(150);
                Timer = 0;
            }         
        }
    }

    public boolean isProtected()
    {
        return protekted;
    }
This is the code of the object that usually kills my main character:
    public void killHarry()
    {
        Harry harry;
        if (isTouching(Harry.class)&& !((Harry)getIntersectingObjects(Harry.class)).isProtected())
        {
            Greenfoot.setWorld(new EndGame());
            Greenfoot.playSound("slurp.wav");
        }
    }
The error message says:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to Harry at Dementor.killHarry(Dementor.java:39) at Dementor.act(Dementor.java:11)
danpost danpost

2018/7/2

#
In the killHarry method, change lines 3 and 4 to the following:
Harry harry = (Harry)getOneIntersectingObject(Harry.class);
if (harry != null && !harry.isProtected())
That should get rid of the error; but you will still have Harry die on first contact with the actor as the code starts an EndGame world without any extra conditions.
grnft grnft

2018/7/3

#
You're right with the error, but I don't know which conditions to use so that it will work.
danpost danpost

2018/7/3

#
grnft wrote...
You're right with the error, but I don't know which conditions to use so that it will work.
What is true when you do not want the game to end? Has health? Has lives? Something else?
grnft grnft

2018/7/3

#
as long as the protected method works: isProtected = true() killDementor() NOT killHarry() of the Dementor class It has only one life, but this one is protected as long as the timer works. Maybe it would be easier to just give him four lifes as soon as he hit the "Tarnumhang class" but this case doesn't work on another object either. Then I would create a healthbar. See another discussion on greenfoot "my extra life doesn't work".
You need to login to post a reply.