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

2018/2/27

How to make a list of objects referring to the superclass?

HardToFindAName HardToFindAName

2018/2/27

#
Hello! So I'm making a game about pollution. I have a superclass named dirt, and a list of subclasses to it. I want to use the function isTouching to check if there is any object of the subclasses from dirt touching a trash can and if there is, I want to check what object it is and then remove it and add or decrease a counter score. That's what I'm trying to do in code:
if (isTouching(dirt.class))
        {
            river world=(river)getWorld();
            Actor x=this.getOneIntersectingObject(dirt.class);
            if (x.equals(ziar.class)||x.equals(carton_cutie.class)){
                world.getObjects(Counter.class).get(0).add(100);
            }
            else{
                world.getObjects(Counter.class).get(0).add(-100);
            }
            world.removeObject(x);
            
        }
something about this. So I want to check if any object that is a waste is touching the trash can, if yes I want to check what type of object it is and based on that increased or decrease the counter, then remove. Help? Edit: There is no object of type dirt in the World.
danpost danpost

2018/2/27

#
Use the following:
if (x instanceof ziar || x instanceof carton_cutie){
HardToFindAName HardToFindAName

2018/2/28

#
danpost wrote...
Use the following:
if (x instanceof ziar || x instanceof carton_cutie){
Thank you very much! It worked great! I have another question if you wouldn't mind helping me: https://www.greenfoot.org/topics/60449/0#post_124333
You need to login to post a reply.