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

2021/1/18

How do you reference an actor's (public) method from the world?

ChafikAZ ChafikAZ

2021/1/18

#
I need to find the opposite of GetWorld().ExampleMethod(). Anyone have an answer or am I grabbing at air?
danpost danpost

2021/1/18

#
ChafikAZ wrote...
I need to find the opposite of GetWorld().ExampleMethod(). Anyone have an answer or am I grabbing at air?
Please elaborate. How many of this type actor can be in the world at one time? Is there always at least one in the world? What is the class name that the actor is created from? What have you tried in your world in an attempt to access the actor and execute its method?
ChafikAZ ChafikAZ

2021/1/19

#
I need to be able to access a certain method from a "Person" actor which only ever has one instance in the world at any one time. I was wondering how to find the actor's equivalent to GetWorld(parameter).BasicExample(parameter), but still haven't caught on whether it's even a thing. Thanks in advance.
danpost danpost

2021/1/19

#
ChafikAZ wrote...
I need to be able to access a certain method from a "Person" actor which only ever has one instance in the world at any one time. I was wondering how to find the actor's equivalent to GetWorld(parameter).BasicExample(parameter), but still haven't caught on whether it's even a thing. Thanks in advance.
Person person = (Person) getObjects(Person.class).get(0);
person.<< method name >>();
or
((Person) getObjects(Person.class).get(0)).<< method name >>();
To avoid the complications, you could just keep a reference to the person in your world:
public class MyWorld extends World
{
    Person person = new Person();
    
    public MyWorld()
    {
        super(600, 400, 1);
        addObject(person, getWidth()/2, getHeight()/2);
        ...
    }
    
    ...
    
    public void whatever()
    {
        ...
        person.<< method name >>();
    ...
ChafikAZ ChafikAZ

2021/1/19

#
Wow, you really are keen on helping wandering chicks like me, thanks!
You need to login to post a reply.