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

2017/4/14

Simple question about sharing variables between classes

AustinProkopishin AustinProkopishin

2017/4/14

#
I need to access a variable from an actor class in my world class and I can't remember what code I have to use to do so.
Yehuda Yehuda

2017/4/14

#
You can have the variable be static, which is not such a good idea because that will mean that it will save the value from last run for this one (only initializes by compilation). The regular way to do it is have an instance of that class. If you have an actor called MyActor with a public int variable in it called myVariable then you can access it by doing:
MyActor actor = new MyActor();

// add it to the world like:
addObject(actor, x, y);

int actorsVariable = actor.myVariable;
AustinProkopishin AustinProkopishin

2017/4/14

#
Ok Thanks!
Yehuda Yehuda

2017/4/14

#
It doesn't have to be in the world for you to access the variable. It may seem so from what I've shown above.
You need to login to post a reply.