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

2014/7/24

Problem with my Project

KTSB KTSB

2014/7/24

#
Hello, I have a problem and I hope that you can help me. My project has an actor class which is called "Person". Moreover the actor class has a subclass which is called "Tent", and this subclass has subclasses,too. Their names are Tent1 and Tent2. The Tent 1 and Tent2 Classes contain a lot of procedures and functions. In my project you can place the person on one of the different tents. Now I would like to find out, in the Person class, on which tent the Person stands and I would like to use the methods of the tent the person stands on. Therefore I have written this code:
     public void Test()
    {
        Tent t = getOneObjectAtOffset(0, 0, Tent.class);
        
        tent.useVoidXXX;   // This is only an example for a tent void;
    
    }
But this code doesn't work. I found out that the first line works with the following code, but then i can't use the functions and procedures of the Tent Classes.
 Actor t = getOneObjectAtOffset(0, 0, Tent.class);
What can I do?
davmac davmac

2014/7/24

#
You can't use the members of the Tent class because the variable is of type Actor. You should declare the variable to be of type Tent (just as you did initially), and use a type cast to assign the result from getOneObjectAtOffset, as follows:
    public void Test()  
    {  
        Tent t = (Tent) getOneObjectAtOffset(0, 0, Tent.class);
          
        tent.useVoidXXX();   // eg
    }  
KTSB KTSB

2014/7/24

#
Thank you very much
You need to login to post a reply.