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

2014/10/25

Lose points whenever object gets eaten

1
2
Coolguy Coolguy

2014/10/25

#
i am currently working on a game, and i am in need of a code that whenever a animal class eats another class,i will lose 1 point from the counter
Super_Hippo Super_Hippo

2014/10/25

#
Do you use the imported counter? Do you have the code so that an animal will eat another class? If you can answer both questions with "yes", then copy the following line to the place where an animal eats another class:
getWorld().getObjects(Counter.class).get(0).add(-1);
Coolguy Coolguy

2014/10/25

#
then i will have to create a "add" method right?
Super_Hippo Super_Hippo

2014/10/25

#
I think there is already an 'add' method in the imported Counter class. If you created the counter yourself, then yes, you have to create such a method.
Coolguy Coolguy

2014/10/25

#
yes indeed , i have the imported counter, but whenever i try to compile i get the cannot find symbol - methode(int)
Super_Hippo Super_Hippo

2014/10/25

#
((Counter)getWorld().getObjects(Counter.class).get(0)).add(-1);
Maybe this will work?
Coolguy Coolguy

2014/10/25

#
it still does not work when i put it in the eat function of the animal
Alwin_Gerrits Alwin_Gerrits

2014/10/25

#
No offense, but typing in ´Greenfoot counter´ in google should give you plenty examples and explanations...
Super_Hippo Super_Hippo

2014/10/25

#
"it does not work"... What is the error message? Can you post the related code?
Coolguy Coolguy

2014/10/25

#
public void eatTrash()
    {
        if ( canSee (Trash.class))
        {
        eat(Trash.class);

            ((Counter) getWorld().getObjects(Trash.class).get(0)).add(-1);  

        } 

        }
Super_Hippo Super_Hippo

2014/10/25

#
Why do you have 'Trash.class' instead of 'Counter.class' in line 7?
Coolguy Coolguy

2014/10/25

#
that was a mistake, i forgot to change it back before copying it
Super_Hippo Super_Hippo

2014/10/25

#
So you have 'Counter.class' there? Does it compile? What is the error message?
Coolguy Coolguy

2014/10/25

#
it does compile , but nothing happens
Super_Hippo Super_Hippo

2014/10/25

#
"Nothing happens" means that the counter doesn't decrease or that the Trash doesn't disappear, too?
There are more replies on the next page.
1
2