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

2013/4/14

How to import my own method?

Kartoffelbrot Kartoffelbrot

2013/4/14

#
Hello everybody, i have written a method. I want to use it by a subclass together with the getOneIntersectingObject() method:
public void transport(Class clss)
    {
        Actor a = getOneIntersectingObject(clss);
        if(a!=null)
        {
            a.moveIntoDirection(getRotation(), speed);
        }
    }
But the the method can't find the moveIntoDirection() method, which i wrote in the superclass. How can i import the moveIntoDirection()-method to the classes I get by getOneIntersectingObject(), so that every greenfoot-class can use it, like the Actor-methods?
Gevater_Tod4711 Gevater_Tod4711

2013/4/14

#
Therefor you need to up this method in all the subclasses of actor because you can't change the code of the actor class.
danpost danpost

2013/4/14

#
Instead of assinging 'a' as type 'Actor', you need to assign it as the type named by your super-class (which I will call 'SuperClass'). Line 3 should be
SuperClass a = (SuperClass) getOneIntersectingObject(clss);
Now, the compiler will look in that class code for the method you wish to run.
Kartoffelbrot Kartoffelbrot

2013/4/14

#
The (SuperClass) in front of getOneIntersectingObject(clss) was the thing I was looking for. Thank you.
You need to login to post a reply.