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

2018/3/19

Problem to create a timer

Kolyma Kolyma

2018/3/19

#
Hello, I have to integrate a timer in my game, from which 15 seconds expire. I've been trying to create this timer for almost 2 hours, but it's not working the way I imagine, and slowly confusing myself with what I programmed. This is the code frome the actor-class Timer:
public class Timer extends Actor
{
    int time = 15;
    private static final Color transparent = new Color(0,0,0,0);
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Timer() 
    {
        updateImage();
    } 
    public void updateImage()
    {
         setImage(new GreenfootImage("Time: " + time, 30, Color.BLACK, transparent));
    }
}
and this from the world Level2:
public class Level2 extends MyWorld
{
    Lives lives = new Lives(); 
    Timer timer = new Timer();
    int time = 15;
    /**
     * Constructor for objects of class Level2.
     * 
     */
    public Level2()
    {
        prepare();
    }
    public Lives getLives()
    {
        return lives;
    }
    public Timer getTimer()
    {
        return timer;
    }
    private void prepare()
    {
        getLives();
        addObject(lives, 730, 40);
        getTimer();
        addObject(timer, getWidth() / 2, 40);
        addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0);
        addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0);
        addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0);
        addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0);
    }
    public void act()
    {
        timeup();
    }
    public void timeup()
    {
        Timer timer = new Timer();
        ((Level2)getWorld()).getTimer().addTime(-1);
    }
}
Can someone help me and tell exactly where my mistakes are?
danpost danpost

2018/3/19

#
Probably need to show World subclass codes.
Kolyma Kolyma

2018/3/19

#
danpost wrote...
Probably need to show World subclass codes.
public class MyWorld extends World { Counter counter = new Counter(); /** * */ public MyWorld() { super(800, 561, 1); prepare(); } public Counter getCounter() { return counter; } /** * Bereitet die Welt zum Start des Spiels vor. */ private void prepare() { addObject( new Mädchen(), this.getWidth() / 2, 490); addObject( new Apple(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Apple(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Apple(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Apple(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Apple(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Banana(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Banana(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Banana(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Banana(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Banana(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0); addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0); getCounter(); addObject(counter, 60, 40); } }
danpost danpost

2018/3/19

#
Oh, well -- I did not see the Level2 class codes given above. But, it helps to see MyWorld also as you have Level2 extending it. This is curious, however it may be what you wanted. The problem is in the Level2 class. First, you do not want to create a new timer every act cycle -- so remove line 39 (in the timeup method). Next, I do not see an addTime method in the Timer class -- where is it? Something else that is strange -- you are calling getCounter, getTimer and getLives from the prepare method unnecessarily -- you can remove those lines.
Kolyma Kolyma

2018/3/19

#
danpost wrote...
Next, I do not see an addTime method in the Timer class -- where is it? Yes, thats a very good question... I'm really confused. I changed my code like this (but I'm sure its false):
Timer timer = new Timer();
    int time = 15;
    /**
     * Constructor for objects of class Level2.
     * 
     */
    public Level2()
    {
        prepare();
    }
    public void Timer()
    {
        return;
    }
    private void prepare()
    {
        getTimer();
        addTime();
        addObject(timer, 730 , 40);
    }
    public void act()
    {
        addTime();
    }
    public void addTime()
    {
       ((Level2)getWorld()).getTimer().addTime(-1);
    }
}
danpost danpost

2018/3/20

#
The only thing I asked you to do in the Level2 class was remove 3 lines (24, 26 and 39 in your first code posting of the class). The addTime method needs to be in the Timer class (with appropriate code).
You need to login to post a reply.