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

2018/3/28

Minute Timer

1
2
3
Lavenger Lavenger

2018/3/30

#
my timer doesnt seem to be running... It's stuck at "hrs 00mins 00sec" whenever i run it here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import greenfoot.*;
public class MyWorld extends World
{
    // fields usually (by convention) go first
    GifImage gifImg = new GifImage("Umaru.gif");
    private Actor TimeDisplay = new SimpleActor();
    private int frames;
    private int Countdown;
    public float AYen; //precise
    public int DYen; //rounded
    public Yen YenDisplay = new Yen();
    public int getHeight = 552 ;
    public int getWidth = 1200 ;
    // class constructors usually (by convention) go next
        public void gifAnimation()
    {
        for (Object obj : gifImg.getImages()) ((GreenfootImage)obj).scale(getWidth(), getHeight());
        setBackground(gifImg.getCurrentImage());
    }
    public MyWorld()
    {
        //For building the world window size
        super(1400, 675, 1);
         
        //For updating the Yen Display
        adjustYen(0); // to initialize image
        prepare();
        addObject(YenDisplay, 1265, 50); // wherever
         
        //For updating the time display
        updateTimeDisplay();
        addObject(TimeDisplay, 1265, 20);
         
        //For triggering the minigame
        Cooldown();
    }    
    // methods (by convention) go last
     
    //For GIF background
    public void act()
    {
        gifAnimation();      
    }
     
    //For Yen Display stuff
    public int getAccumulatedYen()
    {
        return DYen;
    }
    public void adjustYen(float adjustment)
    {
        AYen += adjustment;
        DYen = (int) AYen;
        YenDisplay.setImage(new GreenfootImage("Yen: "+DYen+"¥", 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }
 
    //For Time Display stuff
    private void updateTimeDisplay()
    {
        int Time = frames/55;
        String hours = "0"+(Time/3600);
        String minutes = "0"+(Time/60);
        String seconds = "0"+(Time%60);
        String hrs = hours.substring(minutes.length());
        String mins = minutes.substring(minutes.length()-2);
        String secs = seconds.substring(seconds.length()-2);
        String TimeFormat = "Time: "+hrs+"hrs "+mins+"mins "+secs+"secs"; //Preview: Time: 12345hrs 12mins 12secs
        TimeDisplay.setImage(new GreenfootImage(TimeFormat, 20, Color.BLACK, new Color(0, 0, 0, 0)));
         
    }
     
    //For Choosing the Minigame and timing the appearance of the Minigame
    private void Cooldown()
    {
        int Time = frames/55;
        if (Countdown == 0)
        {
            Countdown += 2+(1 + Greenfoot.getRandomNumber(5)); //chooses a cooldown time between 3-7 minutes randomly    
            Minigame();
        }
        Countdown-=Time/60;
    }
    private void Minigame()
    {
         
    }
     
    //For placing actors into the world
    private void prepare()
    {
        addObject(new Umaru(), 700, 338);
    }
}
danpost danpost

2018/3/30

#
Lavenger wrote...
my timer doesnt seem to be running... It's stuck at "hrs 00mins 00sec" whenever i run it
I would guess so.
danpost wrote...
you need its value to increment every act step.
referring to the frames field.
Lavenger Lavenger

2018/3/30

#
do i just place frames+=1 in the act() method?
danpost danpost

2018/3/30

#
Lavenger wrote...
do i just place frames+=1 in the act() method?
That will get the frames field to increment upward. After which, you need to call the updateTimeDisplay method when needed:
1
if (frames%55 == 0) updateTimeDisplay();
Both can be done in one line of code using the following:
1
if ((++frames)%55 == 0) updateTimeDisplay();
Lavenger Lavenger

2018/3/30

#
danpost wrote...
Lavenger wrote...
do i just place frames+=1 in the act() method?
That will get the frames field to increment upward. After which, you need to call the updateTimeDisplay method when needed:
1
if (frames%55 == 0) updateTimeDisplay();
Both can be done in one line of code using the following:
1
if ((++frames)%55 == 0) updateTimeDisplay();
so i just have to use
1
if ((++frames)%55 == 0) updateTimeDisplay();
in the act() method instead of using
1
2
frames+=1
updateTimeDisplay();
To clarify, this code:
1
if ((++frames)%55 == 0) updateTimeDisplay();
checks if frames is = 0 and if it is, it will call the updateTimeDisplay method which will show the time ever since the game started, is this logic correct?
danpost danpost

2018/3/30

#
Lavenger wrote...
To clarify, this code:
1
if ((++frames)%55 == 0) updateTimeDisplay();
checks if frames is = 0 and if it is, it will call the updateTimeDisplay method which will show the time ever since the game started, is this logic correct?
Almost, you missed on a couple things. You left out that it increments the value of frames first and the check is not if frames = 0. The check is if frames (mod 55) = 0 (if the remainder after dividing frames by 55 is 0).
Lavenger Lavenger

2018/3/30

#
danpost wrote...
Lavenger wrote...
To clarify, this code:
1
if ((++frames)%55 == 0) updateTimeDisplay();
checks if frames is = 0 and if it is, it will call the updateTimeDisplay method which will show the time ever since the game started, is this logic correct?
Almost, you missed on a couple things. You left out that it increments the value of frames first and the check is not if frames = 0. The check is if frames (mod 55) = 0 (if the remainder after dividing frames by 55 is 0).
so once the frames reaches 55, it would stop calling the updateTimeDisplay method since the value of frames is !=0 after being divided by 55?
danpost danpost

2018/3/30

#
Lavenger wrote...
so once the frames reaches 55, it would stop calling the updateTimeDisplay method since the value of frames is !=0 after being divided by 55?
That is not what it suggests.
danpost wrote...
if the remainder after dividing frames by 55 is 0.
When the remainder is zero, frames is divisible by 55. That is when the timer display needs updating -- at the end of every "second".
Lavenger Lavenger

2018/3/30

#
it seems to stop running ater 10 min, do you know to to make it so that it goes on indefinitely?
danpost danpost

2018/3/30

#
Lavenger wrote...
it seems to stop running ater 10 min, do you know to to make it so that it goes on indefinitely?
You have fixed it as suggested here.
Lavenger Lavenger

2018/3/31

#
i'm trying to do a timer than counts down but i'm having difficulties getting it done too... it seems to be stuck at "00mins 00 secs" when its supposed to start out at "3-7mins 00secs" and count down to "00mins 00secs" and start again at "3-7mins 00secs" again This is my current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import greenfoot.*;
public class MyWorld extends World
{
    // fields usually (by convention) go first
    GifImage gifImg = new GifImage("Umaru.gif");
    private Actor TimeDisplay = new TimeDisplay();
    private Actor Cooldown = new Cooldown();
    private int frames;
    private int Cframes;
    private int Countdown;
    public float AYen; //precise
    public int DYen; //rounded
    public Yen YenDisplay = new Yen();
    public int getHeight = 552 ;
    public int getWidth = 1200 ;
    // class constructors usually (by convention) go next
        public void gifAnimation()
    {
        for (Object obj : gifImg.getImages()) ((GreenfootImage)obj).scale(getWidth(), getHeight());
        setBackground(gifImg.getCurrentImage());
    }
    public MyWorld()
    {
        //For building the world window size
        super(1400, 675, 1);
         
        //For updating the Yen Display
        adjustYen(0); // to initialize image
        prepare();
        addObject(YenDisplay, 1265, 50); // wherever
         
        //For updating the time display
        updateTimeDisplay();
        addObject(TimeDisplay, 1265, 20);
         
        //For triggering the minigame
        Cooldown();
        addObject(Cooldown, 1265, 35);
    }    
    // methods (by convention) go last
     
    //For GIF background
    public void act()
    {
        gifAnimation();
        if ((++frames)%55 == 0) updateTimeDisplay();
        if (Cframes <= 0)
        {
            Cframes += (3+Greenfoot.getRandomNumber(5)); //chooses a cooldown time between 3-7 minutes randomly    
            Minigame();
        }
        if ((((--Cframes)%55)/60) == 0) Cooldown();
    }
     
    //For Yen Display stuff
    public int getAccumulatedYen()
    {
        return DYen;
    }
    public void adjustYen(float adjustment)
    {
        AYen += adjustment;
        DYen = (int) AYen;
        YenDisplay.setImage(new GreenfootImage("Yen: "+DYen+"¥", 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }
 
    //For Time Display stuff
    private void updateTimeDisplay()
    {
        int Time = frames/55; // total seconds
        int TotalMinutes = Time/60; // total minutes
        String hours = "0"+(TotalMinutes/60); // string total hours
        String minutes = "0"+(TotalMinutes%60); // string remaining minutes
        String seconds = "0"+(Time%60); // string remaining seconds
        String hrs = hours.substring(hours.length()-2); // hour output string
        String mins = minutes.substring(minutes.length()-2); // minutes output string
        String secs = seconds.substring(seconds.length()-2); // seconds output string
        String TimeFormat = "Time: "+hrs+"hrs "+mins+"mins "+secs+"secs"; //Preview: Time: 12345hrs 12mins 12secs
        TimeDisplay.setImage(new GreenfootImage(TimeFormat, 20, Color.BLACK, new Color(0, 0, 0, 0)));
         
    }
     
    //For Choosing the Minigame and timing the appearance of the Minigame
    private void Cooldown()
    {
        int Time = Cframes/55;
        String minutes = "0"+(Time/60); // string remaining minutes
        String seconds = "0"+(Time%60); // string remaining seconds
        String mins = minutes.substring(minutes.length()-2); // minutes output string
        String secs = seconds.substring(seconds.length()-2); // seconds output string
        String TimeFormat = "Minigame countdown: "+mins+"mins "+secs+"secs"; //Preview: Time: 12345hrs 12mins 12secs
        Cooldown.setImage(new GreenfootImage(TimeFormat, 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }
    private void Minigame()
    {
         
    }
     
    //For placing actors into the world
    private void prepare()
    {
        addObject(new Umaru(), 700, 338);
    }
}
and this are the code i added in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//fields
private Actor Cooldown = new Cooldown();
private int Cframes;
    //in MyWorld()
    Cooldown();
    addObject(Cooldown, 1265, 35);
    //in act()
    if (Cframes <= 0)
    {
        Cframes += (3+Greenfoot.getRandomNumber(5)); //chooses a cooldown time between 3-7 minutes randomly    
        Minigame();
    }
    if ((((--Cframes)%55)/60) == 0) Cooldown();
//new cooldown and minigame methods
private void Cooldown()
{
    int Time = Cframes/55;
    String minutes = "0"+(Time/60); // string remaining minutes
    String seconds = "0"+(Time%60); // string remaining seconds
    String mins = minutes.substring(minutes.length()-2); // minutes output string
    String secs = seconds.substring(seconds.length()-2); // seconds output string
    String TimeFormat = "Minigame countdown: "+mins+"mins "+secs+"secs"; //Preview: Time: 12345hrs 12mins 12secs
    Cooldown.setImage(new GreenfootImage(TimeFormat, 20, Color.BLACK, new Color(0, 0, 0, 0)));
}
private void Minigame()
{
 //to be implemented  
}
Do take note that i have created an actor that have this code inside too:
1
2
3
import greenfoot.*;
 
public class Cooldown extends greenfoot.Actor {}
I have a feeling that its probably this part thats wrong:
1
2
3
4
5
6
if (Cframes <= 0)
{
    Cframes += (3+Greenfoot.getRandomNumber(5)); //chooses a cooldown time between 3-7 minutes randomly    
    Minigame();
}
if ((((--Cframes)%55)/60) == 0) Cooldown();
danpost danpost

2018/3/31

#
You do not need a CoolDown class; and you do not need a TimeDisplay class. You only need the one SimpleActor class. Your CoolDown method should be an updateCoolDownDisplay method. Line 9 should be:
1
private int Cframes; = (3+Greenfoot.getRandomNumber(4))*55;
and line 47 should be:
1
if (Cframes == 0)
and line 52 should be be before that line and look like this:
1
if ((--Cframes)%55 == 0) updateCoolDownDisplay();
In fact, you should probably move the first if inside the second one.
Lavenger Lavenger

2018/3/31

#
danpost wrote...
You do not need a CoolDown class; and you do not need a TimeDisplay class. You only need the one SimpleActor class. Your CoolDown method should be an updateCoolDownDisplay method. Line 9 should be:
1
private int Cframes; = (3+Greenfoot.getRandomNumber(4))*55;
and line 47 should be:
1
if (Cframes == 0)
and line 52 should be be before that line and look like this:
1
if ((--Cframes)%55 == 0) updateCoolDownDisplay();
In fact, you should probably move the first if inside the second one.
so i should delete the Cooldown and TimeDisplay class and implement only one SimpleActor class then Line 6 & 5 would be changed to:
1
2
private Actor TimeDisplay = new SimpleActor();
private Actor Cooldown = new SimpleActor();
and for my act(), it should be:
1
2
3
4
5
6
7
8
9
10
11
public void act()
{
    gifAnimation();
    if ((++frames)%55 == 0) updateTimeDisplay();
    if ((--Cframes)%55 == 0) updateCoolDownDisplay();
    if (Cframes == 0)
    {
        Cframes += (3+Greenfoot.getRandomNumber(5)); //chooses a cooldown time between 3-7 minutes randomly    
        Minigame();
    }
}
along with my Cframes changed to
1
private int Cframes; = (3+Greenfoot.getRandomNumber(4))*55;
Lavenger Lavenger

2018/3/31

#
danpost wrote...
and line 52 should be be before that line and look like this:
line 52 should be before which line?
danpost danpost

2018/3/31

#
Lavenger wrote...
line 52 should be before which line?
Line 47.
There are more replies on the next page.
1
2
3