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

2012/10/9

Variable from world class to actor class

roronoa roronoa

2012/10/9

#
What do I have to do to be able to use a variable from world in one of my actor classes?
danpost danpost

2012/10/9

#
If the name of your sub-class of World was 'MyWorld', and the variable in the world was 'myVar', then in the actor class:
// CASE 1:  if variable in world is 'public'
int myWorldVar = ((MyWorld) getWorld()).myVar;
// CASE 2:  if variable in world is 'private'
int myWorldVar = ((MyWorld) getWorld()).getMyVar();
// with the following method in the 'MyWorld' class
public int getMyVar()
{
    return myVar;
}
Replacements: 'MyWorld' to the name of your sub-class of world 'int' to the variable type needed 'myVar' to the name of the variable in your sub-class of world 'myWorldVar' to whatever you want to call what is returned
You need to login to post a reply.