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

2014/1/18

How to Display a variable

ASC ASC

2014/1/18

#
Hello Greenfoot people, 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" In that class I created a variable named "Timer2" that variable ("Timer2") gets the value of the variable that is in the World I used the folowing code to get the variable from "Timer" (in the world) and place it in "Timer2". I also used a small loop to check if it kept getting the correct value, which it does.
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;
        
        if (smalltimer < 50)
        {
            smalltimer++;
            
        }
        
        if (smalltimer == 50)
        {
            System.out.println(timer2);
            smalltimer = 0;
        }
        
        
    }    
    
My question is, how can I display a variable (that of course keep showing the correct value in"Timer2") ? I plan to use the class "Tijd" just for that. Any help is highly appreciated.
ASC ASC

2014/1/18

#
PS. The variable "Timer2" is the same as the variable "Timer". I just thought that it would be easier if the variable was in the same class. If it's not then you can just ignore that variable ("Timer2").
You need to login to post a reply.