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

2011/7/29

getClass()?

1
2
3
danpost danpost

2011/8/2

#
kiarocks wrote...
are you thinking that textmaker is the world?
Instead of asking a question here, supply the information! Is it the world? and if not, then what kind of class is it? (And do not just say 'yes'; instead, say 'yes, it is the world', or maybe 'no, it is an Actor', or whatever).
davmac davmac

2011/8/2

#
are you thinking that textmaker is the world?
Yes, i was thinking that; it's why I asked: (i.e. TextMaker is your world class?) If it's not then you need to do one of two things: (1) get a reference to a TextMaker instance and then access the txtString variable from that or (2) declare txtString as "static" (public static String txtString = "";) and then access it as "TextMaker.txtWorld". The first option is generally preferable. The second one is less work, but it may cause you some problems (for one thing, if you have more than one TextMaker, they will all share the same value for txtWorld; also, if you press "reset" the value of a static variable will not be reset).
kiarocks kiarocks

2011/8/2

#
no it is not the world. would putting the code there be better?
davmac davmac

2011/8/2

#
And, danpost is right: you need to supply more information. Your questions are vague and difficult to answer. In my previous post, just above, I had to give two possible solutions, because I don't know what you're trying to do exactly nor why. If I had more information I could have spared myself some effort; or, I could have gone into more detail on the solution that is appropriate. I'm less likely to try and help if it takes unnecessary effort, and others surely feel the same way! Questions that spring to mind are:
  • what is TextMaker exactly - is it a world, or an actor, or neither?
  • If TextMaker is an actor type, have you placed one in the world?
  • Can there only ever be one TextMaker or can there more than one?
  • What is TextMaker meant to do, or what does it represent?
  • What does the value of the txtString variable represent or mean?
  • Why exactly do you need to access the "txtString" variable from your SaveButton class?
The point is, you are asking for help, so you should provide as much information as possible so that it is easier for people to help you!
davmac davmac

2011/8/2

#
no it is not the world. would putting the code there be better?
It depends on what you're trying to accomplish. See the questions above.
kiarocks kiarocks

2011/8/2

#
ok, here goes: TextMaker is an actor There is one in the world There can be more than one it represents the text that you type txtString contains the text and i am trying to save a file that contains what you type, when you press SaveButton
mjrb4 mjrb4

2011/8/2

#
That makes things clearer! In your SaveButton class, something like getWorld().getObjects(TextMaker.class) will return a list of all the TextMaker objects in the world. If there's only ever going to be one you can just get the first item in the list returned from that method and use that. "textMaker.txtString" (assuming textMaker is the variable where you have put your object obtained above) will then give you the value of txtString in that object. Since there's only 1 TextMaker in the world you could use the approach davmac outlined above and make it static so it can be accessed from the class. However, not making it static would be the better option since if at a later date you want to add another TextMaker you won't need to refactor (change) your code so much and it should just work.
kiarocks kiarocks

2011/8/2

#
if there is going to be two lines of text, how would i keep it to one object?
mjrb4 mjrb4

2011/8/2

#
Well that's another reason why not using the static approach would be best! But you shouldn't need another object just because there's two lines of text - as long as your TextMaker class can cope with entering a new line. It can still all be stored in the one string; the lines would be separated by new line characters (\n).
kiarocks kiarocks

2011/8/2

#
davmac wrote...
get a reference to a TextMaker instance and then access the txtString variable from that.
How would i get a reference??
mjrb4 mjrb4

2011/8/2

#
How would i get a reference??
Like this:
In your SaveButton class, something like getWorld().getObjects(TextMaker.class) will return a list of all the TextMaker objects in the world. If there's only ever going to be one you can just get the first item in the list returned from that method and use that.
The items in the list are the references to all the "TextMaker"s in the world. If there's just one, then the first (and only) item in the list will be the one you want.
kiarocks kiarocks

2011/8/2

#
but i still get the static error
mjrb4 mjrb4

2011/8/2

#
What do you mean by "static error"? What code gives you this "static error"?
danpost danpost

2011/8/2

#
@kiarocks, create a method in your TextMaker class to return the value of txtString:
public String getTxtString()
{ return txtString; }
Then after you get the reference in SaveButton class to a TextMaker object, call this method to get the value of txtString
List textmakers = getWorld().getObjects(TextMaker.class);
String outTxt =((TextMaker) textmakers.get(0)).getTxtString();
and outTxt has the value of txtString.
kiarocks kiarocks

2011/8/2

#
thanks
There are more replies on the next page.
1
2
3