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

2015/3/11

How do I make global variables?

biferony biferony

2015/3/11

#
How can I make variables between objects? I am trying to make it so when I press a button, it will change a value in an array in another object. Are there better ways to do this? Thanks.
danpost danpost

2015/3/11

#
If you had references to both objects in your world class, as 'objOne' and 'objTwo', you could do this:
1
if (Greenfoot.mouseClicked(objOne)) objTwo.setArrayValue(index, value);
This is only to be taken for its structure, not for its code in particular. Without a better understanding of what you are trying to do, it would be impossible to give anything more precise (if what you trying to do is actually what you need to do).
biferony biferony

2015/3/11

#
danpost wrote...
If you had references to both objects in your world class, as 'objOne' and 'objTwo', you could do this:
1
if (Greenfoot.mouseClicked(objOne)) objTwo.setArrayValue(index, value);
This is only to be taken for its structure, not for its code in particular. Without a better understanding of what you are trying to do, it would be impossible to give anything more precise (if what you trying to do is actually what you need to do).
They are both in Actor classes, though I am willing to change them(if I can find how to do it). The names of the objects are Button1 and CorrectAnswers and the array for the submitted answers is Submitted_Answers.
davmac davmac

2015/3/11

#
The names of the objects are Button1 and CorrectAnswers
Objects do not have names. Those are probably the names of your classes, right?
the array for the submitted answers is Submitted_Answers
Where exactly does this array exist? In what class is it declared? The only real equivalent to "global" variables that Java has is "static" variables. These belong to a class, rather than to objects. So in some class XYZ you can declare a static variable like:
1
public static int my_var = 5;
And then in some other class you can access it using 'XYZ.my_var'.
6716070 6716070

2015/12/16

#
@davmac I can't thank you enough for posting this. I have been trying to figure out how to use static variables for so long. Thank you sir.
You need to login to post a reply.