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

2016/12/6

Score Timer

1
2
HungryHide HungryHide

2016/12/6

#
For the past couple of days, I have been attempting to devise a way for my score to accrue seconds as points, rather than other variables. Of all the methods I have tried, the score will still not use time as its variable. I can always get it to compile but it never actually tracks it on my screen. If anyone has any suggestions, it would be much appreciated.
Wasupmacuz Wasupmacuz

2016/12/6

#
I found that usually I get about 28 acts-per-second. If that helps at all. Maybe you could have an integer that increases by 1 each act cycle. Once it reaches 28 then it changes your timer by 1 and resets itself.
1
2
3
4
5
6
7
8
9
10
public void act()
    {
         APS=APS+1;
         if (APS==28)
        {
            Timer=Timer+1;
            APS=0;
        }
        showText("Score: "+Timer,125,40);
    }
HungryHide HungryHide

2016/12/7

#
@wasupmacuz so can greenfoot recognize APS or should I define it somewhere else? I think you have the right idea
Nosson1459 Nosson1459

2016/12/7

#
You can use this code (with an addObject(new Timer(), x, y) in the World):
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.text.*;
 
/**
 * Write a description of class Timer here.
 *
 * @author (Nosson1459)
 * @version (12/6/2016)
 */
public class Timer extends Actor
{
    private long startTime; // this is the starting time of the scenario
    private long stopTime; // this is for figuring out the diffrence in time from beginning to now
    private double elapsedTime; // this is what timer is equal
    private int a=0;
    GreenfootImage time;
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (a==0)
        {
            startTime=System.currentTimeMillis();
            a=1; // this "if" is so that startTime is initialized only after "Run" is pressed but still only initialized once
        }
        updateTime();
    }   
    private void updateTime()
    {
        stopTime=System.currentTimeMillis(); //gets current time in milliseconds
        elapsedTime=(stopTime-startTime)/1000.0; //converts time to seconds
        time=new GreenfootImage("Timer: " + new DecimalFormat("0.0").format(elapsedTime), 30, Color.WHITE, null, Color.BLACK); // makes the timer image, the decimal format makes the timer only have one decimal place (0.0) (you could do more if you do (0.00) etc.
        setImage(time);
    }
}
I just tested all this now for my first time because you asked, and it all works (after having different problems and solutions).
danpost danpost

2016/12/7

#
A simple way is:
1
2
3
4
5
6
7
private int timer;
 
public void act()
{
    int aps = 28; // change as needed
    if (++timer%aps == 0) showText("Score:  "+(timer/aps), 125, 40);
}
HungryHide HungryHide

2016/12/7

#
Wow, you guys are lifesavers. Thanks so much for the help!! Any suggestions on how to save the time and transfer it from world to world?
danpost danpost

2016/12/7

#
HungryHide wrote...
Any suggestions on how to save the time and transfer it from world to world?
Add the following to any world you need to import the timer value into:
1
2
3
4
public void setTimer(int value)
{
    timer = value;
}
Then, when creating any new world, do it like this (example is for in a World subclass):
1
2
3
MyWorld2 mw2 = new MyWorld2(); // create reference to new world
mw2.setTimer(timer); // set timer in new world
Greenfoot.setWorld(mw2); // activate new world
If activating a new world from an Actor subclass, you will need not only a getter method for the value of the timer in the World subclass, but the actor will need to determine which type of world it is in.
HungryHide HungryHide

2016/12/7

#
@danpost I tried adding it but when i start up my second world, it starts back at 0. Any suggestions?
danpost danpost

2016/12/7

#
HungryHide wrote...
@danpost I tried adding it but when i start up my second world, it starts back at 0. Any suggestions?
Show all the code to all classes that deal with the timer.
HungryHide HungryHide

2016/12/7

#
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color.*;
import java.awt.Font.*;
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        super(600, 400, 1);
        prepare();
    }
    /**
     * Type description here
     */
    private void prepare()
    {
        Fish fish = new Fish();
        addObject(fish, 100, 150);
        Counter counter = new Counter();
        addObject(counter, 70, 30);
        Hand hand = new Hand();
        for(int i = 0; i < 3; i++)
        {
            int x = Greenfoot.getRandomNumber(getWidth() - 1);
            int y = Greenfoot.getRandomNumber(getHeight() - 1);
            addObject(new Hand(), x, y);
        }
    }
    private int timer;
    public void act()
    {
        int aps = 28;
        if (++timer%aps == 0)showText("Score:  "+(timer/aps), 125, 40);
    }
}
 
The lines about spawning a new counter are redundant now.
 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld2 extends World
{
   private int timer;
    /**
     * Constructor for objects of class MyWorld2.
     *
     */
    public MyWorld2()
    {   
        super(600, 400, 1);
        prepare();
         
    }
    /**
     * Type description here
     */
    private void prepare()
    {
        Fish fish = new Fish();
        addObject(fish, 100, 150);
        Counter counter = new Counter();
        addObject(counter, 70, 30);
        Hand hand = new Hand();
        for(int i = 0; i < 5; i++)
        {
            int x = Greenfoot.getRandomNumber(getWidth() - 1);
            int y = Greenfoot.getRandomNumber(getHeight() - 1);
            addObject(new Hand(), x, y);
        }
    }
    public void setTimer(int value)
    {
        MyWorld2 mw2 = new MyWorld2();
        mw2.setTimer(timer);
        Greenfoot.setWorld(mw2);
    }
    public void act()
    {
        int aps = 28;
        if (++timer%aps == 0)showText("Score:  "+(timer/aps), 125, 40);
    }
}
 
^ World 2
 
I think this is it...
danpost danpost

2016/12/7

#
You do not want to create a new MyWorld2 object from a MyWorld2 object. Lines 88 through 90 should simply be:
1
timer = value;
The code you have in lines 88 through 90 belong in the MyWorld class. To test it, replace your act method (lines 39 to 43) with this:
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
{
    int aps = 28;
    if (++timer%aps == 0)showText("Score:  "+(timer/aps), 125, 40);
 
    if (Greenfoot.isKeyDown("space"))
    {
        MyWorld2 mw2 = new MyWorld2();
        mw2.setTimer(timer);
        Greenfoot.setWorld(mw2);
    }
}
Now, run the scenario and press the "space" key to set active a new MyWorld2 world.
HungryHide HungryHide

2016/12/7

#
Ok, now everything works. Any ideas on how I can get greenfoot to change the world at a certain score rather than pressing the space button?
danpost danpost

2016/12/7

#
HungryHide wrote...
Ok, now everything works. Any ideas on how I can get greenfoot to change the world at a certain score rather than pressing the space button?
1
if (timer/aps == levelMaxScore) // replace 'levelMaxScore' with a value
HungryHide HungryHide

2016/12/8

#
@danpost you're the bomb. I really appreciate all the help
Nosson1459 Nosson1459

2016/12/8

#
danpost wrote...
A simple way is: <Code Omitted>
With this code who said the APS is 28, you will have to sit there making sure you have the right amount of APS. With my code it might be longer but it's REAL time. (Was only able to reply now, wasn't here until now.) Just saw HungryHide's post (after posted mine).
There are more replies on the next page.
1
2