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

2017/4/19

Countdown doesnt work

7oel 7oel

2017/4/19

#
My countdown code, immediately switches to 2 and stays stuck, i do not know why.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Countdown here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Countdown extends Actor
{
    private GreenfootImage Countdown3 = new GreenfootImage("Countdown3.png");
    private GreenfootImage Countdown2 = new GreenfootImage("Countdown2.png");
    private GreenfootImage Countdown1 = new GreenfootImage("Countdown1.png");
    private GreenfootImage CountdownGo = new GreenfootImage("CountdownGo.png");
    private static final int countdowntime = 50;
   private int countdowntimer = Integer.MAX_VALUE;
    /**
     * Act - do whatever the Countdown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Countdown3();
    }
    public void Countdown3()
    { 
        if(countdowntimer > countdowntime)
        {
           Countdown2();
           
        }
        else
        {
            countdowntimer++;
        }
    }
    public void Countdown2()
    {
        setImage(Countdown2);
        countdowntimer = 0;
        if(countdowntimer > countdowntime)
        {
            Countdown1();
        }
        else
        {
            countdowntimer++;
        }
    }
    public void Countdown1()
    {
        setImage(Countdown1);
        countdowntimer = 0;
        if(countdowntimer > countdowntime)
        {
            CountdownGo();
        }
        else
        {
            countdowntimer++;
        }
    }
    public void CountdownGo()
    {
        setImage(CountdownGo);
        countdowntimer = 0;
        if(countdowntimer > countdowntime)
        {
            World world;
            world=getWorld();
            world.removeObject(this);
            
        }
        else
        {
            countdowntimer++;
        }
    }
    
    
    
}
danpost danpost

2017/4/19

#
Your Countdown2, Countdown1 and CountdownGo methods all set countdowntimer to zero every time they are called; so, they will never be 50 in any of those. However, you have other issues with the way you are treating the countdowntimer field. I will edit post to explain. EDIT: By resetting the value of countdowntimer to zero, you are resetting the entire countdown. Let the value continue past 50 and begin each of the methods aforementioned with lines like the following:
if (countdowntimer > 100)
{
    Countdown1();
    return;
}
Use 150 in Countdown1 and 200 in CountdownGo adjusting what method they should proceed to. You could alternatively use 'countdowntime*2' for '100', etc.
7oel 7oel

2017/4/20

#
DO i still use the else?
danpost danpost

2017/4/20

#
7oel wrote...
DO i still use the else?
Yes.
7oel 7oel

2017/4/20

#
danpost wrote...
Your Countdown2, Countdown1 and CountdownGo methods all set countdowntimer to zero every time they are called; so, they will never be 50 in any of those. However, you have other issues with the way you are treating the countdowntimer field. I will edit post to explain. EDIT: By resetting the value of countdowntimer to zero, you are resetting the entire countdown. Let the value continue past 50 and begin each of the methods aforementioned with lines like the following:
if (countdowntimer > 100)
{
    Countdown1();
    return;
}
Use 150 in Countdown1 and 200 in CountdownGo adjusting what method they should proceed to. You could alternatively use 'countdowntime*2' for '100', etc.
It doesnt work. It doesnt get past the 3 now
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Countdown here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Countdown extends Actor
{
    private GreenfootImage Countdown3 = new GreenfootImage("Countdown3.png");
    private GreenfootImage Countdown2 = new GreenfootImage("Countdown2.png");
    private GreenfootImage Countdown1 = new GreenfootImage("Countdown1.png");
    private GreenfootImage CountdownGo = new GreenfootImage("CountdownGo.png");
    private static final int countdowntime = 50;
   private int countdowntimer = Integer.MAX_VALUE;
    /**
     * Act - do whatever the Countdown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        countdowntimer = 0;
        Countdown3();
    }
    public void Countdown3()
    { 
        countdowntimer++;
        if(countdowntimer > 50)
        {
           Countdown2();
           return;
        }
        
    }
    public void Countdown2()
    {
        setImage(Countdown2);
        countdowntimer++;
        if(countdowntimer > 100)
        {
            Countdown1();
            return;

        }
        
    }
    public void Countdown1()
    {
        setImage(Countdown1);
        countdowntimer++;
        if(countdowntimer > 150)
        {
            CountdownGo();
            return;
        }
        
    }
    public void CountdownGo()
    {
        setImage(CountdownGo);
        countdowntimer++;
        if(countdowntimer > 200)
        {
            World world;
            world=getWorld();
            world.removeObject(this);
            

        }
        
    }
    
    
    
}
7oel 7oel

2017/4/20

#
danpost wrote...
7oel wrote...
DO i still use the else?
Yes.
Didnt see this oops
7oel 7oel

2017/4/20

#
This still doesn't work, it just gets stuck on 3
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Countdown here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Countdown extends Actor
{
    private GreenfootImage Countdown3 = new GreenfootImage("Countdown3.png");
    private GreenfootImage Countdown2 = new GreenfootImage("Countdown2.png");
    private GreenfootImage Countdown1 = new GreenfootImage("Countdown1.png");
    private GreenfootImage CountdownGo = new GreenfootImage("CountdownGo.png");
    private static final int countdowntime = 50;
   private int countdowntimer = Integer.MAX_VALUE;
    /**
     * Act - do whatever the Countdown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        countdowntimer = 0;
        Countdown3();
    }
    public void Countdown3()
    { 
        
        if(countdowntimer > 10)
        {
           Countdown2();
           return;
        }
        else
        {
            countdowntimer++;
        }
        
    }
    public void Countdown2()
    {
        setImage(Countdown2);
        
        if(countdowntimer > 20)
        {
            Countdown1();
            return;

        }
        else
        {
            countdowntimer++;
        }
        
    }
    public void Countdown1()
    {
        setImage(Countdown1);
        
        if(countdowntimer > 30)
        {
            CountdownGo();
            return;
        }
        else
        {
            countdowntimer++;
        }
        
    }
    public void CountdownGo()
    {
        setImage(CountdownGo);
        
        if(countdowntimer > 40)
        {
            World world;
            world=getWorld();
            world.removeObject(this);
            

        }
        else
        {
            countdowntimer++;
        }
        
    }
    
    
    
}
danpost danpost

2017/4/20

#
Remove line 23 and change line 16 to:
private int countdowntimer;
7oel 7oel

2017/4/21

#
Thanks!
You need to login to post a reply.