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

2012/4/11

Moving Between Worlds.

Dingram Dingram

2012/4/11

#
How would I go about changing a variable in the World from within an Actor? Thanks in advance.
danpost danpost

2012/4/11

#
If you called your sub-class of World 'MyWorld', then '((MyWorld) getWorld()).variableName = whateverValue;' or you could write some methods to 'public void setVariable(int value)' and 'public void adjustVariable(int amount)' and have one to retrieve the value with 'public int getVariable()'; but you would still need to do '(MyWorld) getWorld()' or
MyWorld mw;
mw = (MyWorld) getWorld();
mw.adjustVariable(-1);
if (mw.getVariable() == 0) doWhatNotGood();
(above is just as an example).
Dingram Dingram

2012/4/11

#
If I am then comparing the variable with something in a sub-class of the world in which it is stored in how would I go about that? Would it just be "if(VARIABLE == 'x')" or would it need to be directed to the parent class?
danpost danpost

2012/4/12

#
It would need to be directed to the parent class. It would be (with 'MyWorld' as a sub-class of World ):
if (((MyWorld) getWorld()).VARIABLE == 'x')
The variable VARIABLE cannot be 'private' or access to it will be denied, and you will have to either make it public or write the aforementioned methods and use them for access.
You need to login to post a reply.