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

2013/3/7

How to use a variable instance from a Actor class in the world?

bcbrandon3 bcbrandon3

2013/3/7

#
Hi im new at Greenfoot and have been doing okay but am having a big problem with making a variable in a class be able to be checked an use by the world. i am making a game for class and its pretty much like frogger... When you touch one side you gain a point and touch the other another point and so on. But i wanted to use that variable to be when it is great then 10 to use my codes that increases the amount of cars that come across the screen. Ive looked through tons of stuff and looked at the Space ship tut and it confused me so much becasue im not using a counter or even an class just a variable?
danpost danpost

2013/3/7

#
In the tutorial the Space class is the class, and 'theCounter' is the field holding a Counter object of a world object. In your scenario, the Frog class is the class and 'score' if the field holding an int value of a Frog object. Getting the int value of 'score' from the Frog object into your world class is done the same way as the tutorial gets the object of 'theCounter' from the world object into the Shot class.
bcbrandon3 bcbrandon3

2013/3/7

#
Oh so im pretty much visualizing my variable as there class Counter? but what im confused about is this code:
public Counter getCounter()
{
    return theCounter;
}
and where/ what i would use for my variable which is public int SidesTagged; in the frog class?
danpost danpost

2013/3/7

#
Yes. The word 'Counter' is the return type (the type of value returned; in this case a Counter object). You will be returning an int value, so will use 'int' as the return type.
bcbrandon3 bcbrandon3

2013/3/7

#
Sorry for the very late response but after using that what would i do in the world to be able to use the variable? im not understanding what needs to be done from there...?
danpost danpost

2013/3/7

#
I guess you would use (in the world act method or a method it calls):
Frog frog = (Frog) getObjects(Frog.class).get(0); // get frog
int tagged = frog.getSidesTagged(); // get score
int factor = tagged/10; // get factor
and use 'factor' times some nominal value to add to the initial number of cars. In my frogger remake, I think I used the number of passes as a factor to increase the random chance of vehicles being added to the world.
You need to login to post a reply.