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

2020/3/25

Change variable in world from actor?

whattheheck whattheheck

2020/3/25

#
Say my world class contains a variable x, how would I be able to update this variable from within an actor class? I have tried something like this:
public class MyWorld extends World
{
    public int worldVar;
}

public class actClass extends Actor
{
    (MyWorld) getWorld().worldVar += 10;
}
However when I do this sort of method, I receive a null pointer exception. Is there something wrong with this method, or is there another way to change a world variable from within an actor?
danpost danpost

2020/3/25

#
You need to bracket (parenthesize) what is being typecast to MyWorld if you are to access a member (field or method) of the class on the same line:
((MyWorld)getWorld()).worldVar += 10;
You need to login to post a reply.