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

2017/3/30

Testing an Act Method

rockon411 rockon411

2017/3/30

#
Is there anyway to test an act method for a certain number of turns. My actor does something every 100 turns and it would really convenient if I could set the counter in the test method or something similar.
 public void act()
    {
        counter++;
        if (counter == 40)
        {
            counter = 0;
            move();
        }
    }
Here is my act set up. Any help would be greatly appreciated.
danpost danpost

2017/3/30

#
I think what you are asking is for a why to use the current counter for multiple tasks. Like this:
public void act()
{
    counter++;
    if (counter%100 == 0)
    {
        // do something (like you said)
    }
    if (counter%40 == 0)
    {
        move();
    }
    if (counter == 200)
    {
        counter = 0;
    }
}
We can reset the counter when it reaches 200 because it is a multiple of both 40 and 100.
rockon411 rockon411

2017/3/30

#
I'm actually looking more for a way to set the counter in a test class.
public void testActCounter()
    {
        counter = 120;
        ((ThrowerAnt)ant).act();

    }
This doesn't seem to work however.
Nosson1459 Nosson1459

2017/3/30

#
You should then either have it as a parameter in the constructor method or you can have a setter method in ThrowerAnt which you'll then need a way to access it.
rockon411 rockon411

2017/3/30

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