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

2014/1/19

Need help displaying a variable !!!

ASC ASC

2014/1/19

#
Hello Greenfoot Wizards, I need some help displaying a variable in the world. I know that I need to do it with an actor, and I know how to do that with static text, But I don't know how to do it when it's a variable. The variable is "Timer", which is in the World class ("Haven"). I created a class named "Tijd" If it's easier to display a variable that is in the same class, than that would be fine too. so I also created a variable named Timer2 which holds the same value.
int smalltimer = 0;  
      
    /** 
     * Act - do whatever the Tijd wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
    public void act()   
    {  
        int timer2 = ((Haven)getWorld()).timer;  
     
          
    }      
My question is, how can I display a variable (that of course keep showing the correct value in"Timer or Timer2") ? I plan to use the class "Tijd" just for that. Any help is highly appreciated.
Gevater_Tod4711 Gevater_Tod4711

2014/1/19

#
When you already have the right value of the variable you just need to set the actors image to that variable:
    public void act() {    
        int timer2 = ((Haven)getWorld()).timer;    
        setImage(new GreenfootImage(""+timer2, 35, null, null);
        //this will make the actors image become the value of timer2 with a fontsize of 35, a black text color and a transparent background color.
       // you can also change this settings by changing the parameters 35, null, null;
    }
ASC ASC

2014/1/19

#
Thanks, that worked. I did know how to display a string in greenfoot, but simple replacing the string with an variable strangely doesn't work. But I see that adding a variable to an ( In this case) empty string does work, So thanks again, also for the quick reply.
You need to login to post a reply.