1 | MyWorld world = (MyWorld) getWorld();
|
what does it does? i have written this in my actor class but don't know what it does?
The line is equivalent to:
1 2 | MyWorld world;
world = (MyWorld) getWorld();
|
where line 1 declares a new variable, world, that can contain a reference to a
MyWorld object (an instance of
MyWorld) and line 2 assigns a
MyWorld object to the variable by getting a reference to the
World object the actor is in (using the
getWorld method, which returns a reference of a
World object -- or
null), then stating that the reference is, in particular, a
MyWorld object (by casting:
(MyWorld)) so the reference can be assigned to the variable.
The complexity of your line of code is only necessary when you need to access a method or field that is declared in your
MyWorld class. If you are just using methods given in the
World class, you only need to code this:
1 | World world = getWorld();
|
That is, if you were to declare a variable at all, as you could just chain the method you wanted to use with the
getWorld method, like, for example:
1 | int cs = getWorld().getCellSize();
|
or
1 | if (getWorld().getObjects(Enemy. class ).isEmpty())
|
(to string more methods).
Of course, you can also string your assigned value, like, for example:
1 | int n = ((MyWorld)getWorld()).getCounter().getValue();
|