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

2021/11/21

How can a share a variable between world class an actor class

KasB KasB

2021/11/21

#
How can a share a variable between world class and actor class
danpost danpost

2021/11/21

#
KasB wrote...
How can a share a variable between world class and actor class
Use: ((MyWorld)getWorld()). followed by variable name.
KasB KasB

2021/11/21

#
It doesn't worrk
KasB KasB

2021/11/21

#
I have a variable nrOfSteps in the subclass man of the class Actor, which must go to the subclass Level 1 of class world
KasB KasB

2021/11/21

#
I've been working on it for quite some time but it just won't work.
danpost danpost

2021/11/21

#
KasB wrote...
I have a variable nrOfSteps in the subclass man of the class Actor, which must go to the subclass Level 1 of class world
Okay. I had the variable being passed the wrong way (misled by title). Keep a reference to the man actor in your Level1 class so you can use man.nrOfSteps.
KasB KasB

2021/11/21

#
How do I do that? Can you give an example, because I just started working with greenfoot for a school project?
KasB KasB

2021/11/21

#
I tried this: <class>.<variable> = <value> but it didn't work. Its says: non-static variable connot be referenced from a static context
danpost danpost

2021/11/21

#
KasB wrote...
How do I do that? Can you give an example, because I just started working with greenfoot for a school project?
//outside method in world (Level1)
Man man = new Man();

// inside method in actor
<class> <variable name> = ((Level1)getWorld()).man.<variable>;
//example
int steps = ((Level1)getWorld()).man.nrOfSteps;
(always begin class names with uppercase letters and variable names with lowercase letters -- as per convention). Make sure that the Man object created here is the one that is placed into the world -- not another created Man object.
KasB KasB

2021/11/21

#
Oke, thanks for your help
You need to login to post a reply.