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

2012/4/13

Counter with an actor: Get Value

PStigerID PStigerID

2012/4/13

#
After following this tutorial: http://www.greenfoot.org/scenarios/2359 I now need to get the value of the counter somehow into another actor. So that it is an int. I used this method for speed of a character. So it must work with move(); Any idea? Thx PStiger
danpost danpost

2012/4/13

#
If you are using the counter for the speed of the character, then I will assume that as long as the character is in the world, the speed counter is, also. Therefore, during world creation, create the counter first, assigning it to a Counter variable. Then add it to the world. Next, create the character, passing it the reference to the counter. Example:
Counter counter = new Counter();
addObject(counter, 100, 100);
Character character = new Character(counter);
addObject(character, 200, 200);
Now, all you need is a Character constructor that recieves a Counter object as a parameter and a way to save the counter:
Counter counter;

public Character(Counter c)
{
    counter = c;
}
From now on, in the Character class, you can just say 'int counterValue = counter.getValue();', or 'move(counter.getValue());', or whatever.
PStigerID PStigerID

2012/4/13

#
Perfect! Thanks
You need to login to post a reply.