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

2019/3/22

When do I use World. and when getWorld().

RedMine360 RedMine360

2019/3/22

#
My first question is the one mentioned in the title. So why can't i write (in my Actor Arrow)
World.removeObject(this);
My second question is why do i need the (Stickman) ? I tried it like this:
public int StickmanX()
    {
        Stickman stickman = getWorld().getObjects(Stickman.class).get(0);
        return stickman.getX();
    }
but it only works if i do it like this:
public int StickmanX()
    {
        Stickman stickman = (Stickman)getWorld().getObjects(Stickman.class).get(0);
        return stickmant.getX();
    }
but what does the (Stickman) do?
danpost danpost

2019/3/23

#
RedMine360 wrote...
My first question is the one mentioned in the title. So why can't i write (in my Actor Arrow)
World.removeObject(this);
World is a Class object name. You do not add to or remove actors from a class. You add to and remove actors from a World instance (a World object). You can have multiple World objects in your project. Not only that, you may use multiple instances of the same type world. You need to be specific as to which World object the work is being done in (particularly from an Actor subclass).
My second question is why do i need the (Stickman) (I'm still in Arrow)? I tried it like this: << Code Omitted >> but what does the (Stickman) do?
The getObjects method, by default, returns a list of Object objects. You need to specify that the element is a Stickman instance (or at least an Actor instance) before calling getX on it (or the compiler will only look in the Object class for the method and will not find it there).
RedMine360 RedMine360

2019/3/23

#
ok makes sense now :D thx
You need to login to post a reply.