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

2019/5/7

Passing variable in World to an actor

pawr pawr

2019/5/7

#
i can't seem to figure out how to pass the variable to an actor here is the variable declared in MyWorld
String college=JOptionPane.showInputDialog("Enter college: ");
how to share this variable to a actor?
ScaleFlaw89 ScaleFlaw89

2019/5/7

#
If you are trying to put that as the name of the college, wherever you declared the name (for ex: collegeName) you would need to do
college = collegeName;
//then declare you name to whatever college you created
methodName  = new Method (collegeName);

//then declare it in your world setup

add(new Location(int x, int y), methodName);
Hope this helps. Took me awhile to figure this out myself :P
Super_Hippo Super_Hippo

2019/5/7

#
It could look like this:
//in world when adding the object to the world
A a = new A();
a.setCollege(college);
addObject(a, x, y);
//in class A
private String college;

public void setCollege(String col)
{
    college = col;
}
If the actor is already in the world, you need to get a reference to it and then call the setCollege method on it. Or you save a reference to it when you create the object and use this reference when you need it.
pawr pawr

2019/5/9

#
uhm... how do I get the stored input in the variable college and call the variable in an actor? for example: //in the actor setText("College: " + college); //the college variable is from MyWorld, how do I get that variable and use it in an actor?
pawr pawr

2019/5/9

#
ScaleFlaw89 wrote...
If you are trying to put that as the name of the college, wherever you declared the name (for ex: collegeName) you would need to do
college = collegeName;
//then declare you name to whatever college you created
methodName  = new Method (collegeName);

//then declare it in your world setup

add(new Location(int x, int y), methodName);
Hope this helps. Took me awhile to figure this out myself :P
uhm... how do I get the stored input in the variable college and call the variable in an actor? for example: //in the actor setText("College: " + college); //the college variable is from MyWorld, how do I get that variable and use it in an actor? (p.s the actor is not inside MyWorld)
You need to login to post a reply.