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

2018/12/3

showText not working in Actor Class

ouy8 ouy8

2018/12/3

#
I've made a rudimentary Timer that's a subclass of Actor. I originally had the "showText" bit at the end of the updateTime() method, but since that didn't work I tried it in the Act method with the same result. The code works just fine if it's inside my World class, but it doesn't work inside the Timer one. What's the best way to get the text to display?
import greenfoot.*;

/**
 * Write a description of class Timer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer extends Actor
{
    int numOfActCycles;
    int minutes;
    int seconds;
    String timeText;
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        numOfActCycles--;
        updateTime();
        getWorld().showText("Time: " + timeText, 55, 10);
    }    

    /*****************************************
     * Updates and displays the time left in 0:00 format.
     */
    public void updateTime()
    {
        if(numOfActCycles >= 3600 )
            minutes = 1;
        else minutes = 0;

        if(numOfActCycles % 60 == 0)
            seconds -= 1;

        timeText = minutes +":" + seconds;

        if(seconds < 10)
            timeText = minutes + ":0" + seconds;

        if(seconds == -1)
            seconds = 59;
    }
}
danpost danpost

2018/12/3

#
ouy8 wrote...
I've made a rudimentary Timer that's a subclass of Actor. I originally had the "showText" bit at the end of the updateTime() method, but since that didn't work I tried it in the Act method with the same result. The code works just fine if it's inside my World class, but it doesn't work inside the Timer one. What's the best way to get the text to display? << Code Omitted >>
Your numOfActCycles field starts at zero (line 11), then is continually decreased (line 21). Its value will never be greater than or equal to 3600 (line 31).
ouy8 ouy8

2018/12/3

#
Ooh rookie mistake. I fixed that tidbit so that it initializes to 7200 (it's only going to count off two minutes), but it still refuses to show up with showText.
danpost danpost

2018/12/3

#
ouy8 wrote...
Ooh rookie mistake. I fixed that tidbit so that it initializes to 7200 (it's only going to count off two minutes), but it still refuses to show up with showText.
Does it show up after you click the 'Run' or "Act' button?
ouy8 ouy8

2018/12/3

#
It does not. Adding it into the World via the constructor puts it in the exact spot no matter where I place it on the screen and has it working as intended. My main concern here is that I have two Worlds that represent two levels and want the Timer to remain consistent when it changes from level 1 to level 2.
danpost danpost

2018/12/3

#
ouy8 wrote...
My main concern here is that I have two Worlds that represent two levels and want the Timer to remain consistent when it changes from level 1 to level 2.
It would require taking the same Timer object into the next world. Either that, or set the states of the new Timer object to those of the old one. Re-writing the Timer class would help in the second case as minutes, seconds and timeText are always dependent on the current value of numOfActCycles
ouy8 ouy8

2018/12/3

#
I ended up just making a Timer object in the first level that is passed as an argument to the second level's constructor and it all seems to be working well enough (for now). Thank you for your help!
You need to login to post a reply.