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

2015/2/17

Dedect collision with class and subclass

leuveg leuveg

2015/2/17

#
Hey there, i have search for a solution but i do not found them. I think i have used the wrong search key words, but i have no idea what i should do search for. Well, the question is: How can i realize a general collisions dedection for all fruits? I have this objects: Mr_Wombat Fruit +-Bannana +-Appel +-Orange +-... Now i used this in the Mr_Wombat.act()
if (getOneIntersectingObject(Bannana.class) != null){
            countPoints();
        }
It works, but i will put more fruits in the game and i think it is not posible when i use the redundant code for each fruit like this
if (getOneIntersectingObject(Bannana.class) != null){
            countPoints();
        }

if (getOneIntersectingObject(Appel.class) != null){
            countPoints();
        }
if (getOneIntersectingObject(Orange.class) != null){
            countPoints();
        }

...
Easyer solution? I think at this
if (getOneIntersectingObject(Fruits.class) != null){
            countPoints();
        }
But this dont work.
davmac davmac

2015/2/17

#
But this dont work.
It should work. Are you certain it doesn't?
joandvgv joandvgv

2015/2/17

#
 
1
2
3
if (getOneIntersectingObject(Fruits.class) != null){
            countPoints();
        } 
I think this works but does something completely different to what you want. You want it to "getOneInterectingObject" from the superclass and the subclasses. But I think it just get you the ones from the superclass. Remember Banana.class may be a subclass of Fruits, but it is a totally different object. I mean, we may be the children of our parents but we're not like them. though I'm not 100% sure about it, it might help you find your solution.
danpost danpost

2015/2/17

#
How is it not working correctly. Is it compiling? is it running? do you get an error message and if so what is it? Is the class named 'Fruit' or 'Fruits'?
davmac davmac

2015/2/17

#
I think this works but does something completely different to what you want. You want it to "getOneInterectingObject" from the superclass and the subclasses. But I think it just get you the ones from the superclass.
That's not right, it will get all instances of the given class (including instances of subclasses).
fejfo fejfo

2015/2/18

#
I think danpost spoted the mistake you have the class Fruit but you tryi to get the objects of the class Fruits
joandvgv joandvgv

2015/2/18

#
davmac wrote...
I think this works but does something completely different to what you want. You want it to "getOneInterectingObject" from the superclass and the subclasses. But I think it just get you the ones from the superclass.
That's not right, it will get all instances of the given class (including instances of subclasses).
Ok, thanks for the clarification.
You need to login to post a reply.