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

2018/7/1

adding the amount of removed objects to my score counter

grnft grnft

2018/7/1

#
My actor hits an object and some other objects (the enemies) are removed caused by that inicident.
    public void zaubern()
    {
        if (isTouching(Elderstab.class)) {
            removeTouching(Elderstab.class);
            Dementor dementor =  new  Dementor();
            getWorld().removeObjects(getWorld().getObjects(Dementor.class))
};
My objects are gone now, but I need them to be added to my score Counter
public class Counter extends Actor
{
    int newScore = 0;
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + newScore, 25, Color.WHITE, Color.BLACK));
    }    

    public void addScore()
    {
        newScore++;
    }
How I add some points to the score (from another class, this is only an example):
            World myWorld = getWorld();
            Hogwarts hogwarts = (Hogwarts)myWorld;
            Counter counter = hogwarts.getCounter();
            counter.addScore();
danpost danpost

2018/7/1

#
grnft wrote...
My actor hits an object and some other objects (the enemies) are removed caused by that inicident. << Code Omitted >>
Line 5 in the zaubern method can be removed. I do not see why you would create an object to remove objects of its type from the world.
My objects are gone now, but I need them to be added to my score Counter << Code Omitted >> How I add some points to the score (from another class, this is only an example): << Code Omitted >>
Replace lines 5 and 6 in the zaubern method with the following:
Counter counter = ((Hogwarts)getWorld()).getCounter();
for (Object obj : getWorld().getObjects(Dementor.class))
{
    getWorld().removeObject((Actor)obj);
    counter.addScore();
}
grnft grnft

2018/7/1

#
You're a genius!
You need to login to post a reply.