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

2014/5/5

Timer

jmdreyer jmdreyer

2014/5/5

#
In my program my timer starts right when I compile the game instead of when I run the game. What should I do. Here is the code.
 private int second = 0;
    public Darkness()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 500, 1); 
        addObject(new Marble(), 250, 150);
        addObject (new Timer(), 250, 250);
        populateWorld();
    }

    public void populateWorld()
    {
        for ( int count = 0; count < 1; count++)
        {
            int x =  Greenfoot.getRandomNumber (450);
            int y = 450;
            addObject (new Glass(), x, y);
        }
    }     
    public void act()
    {
        second++;
        if (second == 9)
        {
            int x =  Greenfoot.getRandomNumber (450);
            int y = 450;
            addObject (new Glass(), x, y);
            second = 0;
        }
    }
}
erdelf erdelf

2014/5/5

#
you should give "second" a value in the constructor instead of during class declaration
danpost danpost

2014/5/5

#
You should remove line 7 and add the following method to the class code:
public void started()
{
    if (getObjects(Timer.class).isEmpty()) addObject(new Timer(), 250, 250);
}
danpost danpost

2014/5/5

#
erdelf wrote...
you should give "second" a value in the constructor instead of during class declaration
It does not matter where, or even IF, you give 'second' a value. All primitive (int, char, double, long, float, byte, and short) numeric fields are automatically given a value of zero by default when declared (booleans are automatically given a 'false' value).
erdelf erdelf

2014/5/5

#
i didnt read his code correctly i'm sorry
jmdreyer jmdreyer

2014/5/7

#
thank you guys for your help
You need to login to post a reply.