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

2016/12/6

Score Timer

1
2
danpost danpost

2016/12/8

#
Nosson1459 wrote...
With this code who said the APS is 28, you will have to sit there making sure you have the right amount of APS.
Nobody said it had to be 28. That is why I singled out the value in line 5 and indicated it could be changed as needed.
With my code it might be longer but it's REAL time.
Real time is not necessarily better than counting act cycles. There is no compensation in real time when you change the speed of the scenario and, not only that, if the scenario is paused and restarted, the time difference is not accounted for.
Nosson1459 Nosson1459

2016/12/8

#
danpost wrote...
Real time is not necessarily better than counting act cycles.
If you count act cycles it's not for sure a perfect ratio to seconds, so I could be playing a game for five minutes and it might say ten minutes.
danpost wrote...
There is no compensation in real time when you change the speed of the scenario and, not only that, if the scenario is paused and restarted, the time difference is not accounted for.
With real time no matter what you change the speed to a second will still be a second, (but if you make it very slow it will take a long time to get to the coding for changing the image) but with APS you'll have to change the ratio if the speed was changed. I'm trying to think of a solution to the counting time when stopped.
danpost danpost

2016/12/8

#
Nosson1459 wrote...
If you count act cycles it's not for sure a perfect ratio to seconds, so I could be playing a game for five minutes and it might say ten minutes.
That is why you adjust the value of 'aps' to the speed of the scenario. So, it will approximate seconds.
Nosson1459 Nosson1459

2016/12/8

#
danpost wrote...
That is why you adjust the value of 'aps' to the speed of the scenario. So, it will approximate seconds.
Well then there is a lot of figuring out to make sure you have the right APS (and you'll have to put a setSpeed(int) to make sure the scroll bar isn't changed a bit). Also, each addition to the scenario can make it slower so you'll have to re figure it out. (Can you look at the new discussion I made? (I didn't ask the question here just in case you'll tell me "to start a new discussion thread on the topic".))
danpost danpost

2016/12/8

#
Nosson1459 wrote...
Well then there is a lot of figuring out to make sure you have the right APS (and you'll have to put a setSpeed(int) to make sure the scroll bar isn't changed a bit). Also, each addition to the scenario can make it slower so you'll have to re figure it out.
Additions to a scenario do not slow it down unless you are processing too much in an act cycle. Greenfoot will add a "delay" between the processing of each act cycle to regulate the speed. You would normally be just taking some of the delay time away. Of course if you have a excess amount of processing to do in one act cycle, taking away all its delay time, it will then cause lag in your project. You can test this by moving the speed slider to the right. At some point, the processing will take the required time for one act and the apparent speed of the scenario will not increase any further (even though you can still increase the slider). This is usually most apparent in scrolling worlds or worlds with an abundance of actors. As far as changing the speed slider and aps adjusting: the user is not usually given the ability to change the speed of a scenario. Therefore, you only would have to adjust the aps when you, yourself, change the speed of the scenario.
Nosson1459 Nosson1459

2016/12/8

#
But basically it can slow down even after the calculations.
danpost wrote...
As far as changing the speed slider and aps adjusting: the user is not usually given the ability to change the speed of a scenario. Therefore, you only would have to adjust the aps when you, yourself, change the speed of the scenario.
I know the user is usually blocked from doing that , but in the Greenfoot program on my computer, not the website, if I by mistake move it or someone else in the house does, then either I have the setSpeed(), or I recalculate the aps.
danpost danpost

2016/12/8

#
Nosson1459 wrote...
in the Greenfoot program on my computer, not the website, if I by mistake move it or someone else in the house does, then either I have the setSpeed(), or I recalculate the aps.
or just move it back.
Nosson1459 Nosson1459

2016/12/8

#
danpost wrote...
or just move it back.
There are no numbers on the scroll bar, just 5 lines 1 in the beginning, 1 at the end, 1 at 25, 1 at 50, and 1 at 75 (When I hover my mouse over it, it says "Adjusts the execution speed").
Nosson1459 Nosson1459

2016/12/8

#
(This whole scroll bar talk is just branching off the above posts.)
danpost danpost

2016/12/8

#
Nosson1459 wrote...
danpost wrote...
or just move it back.
There are no numbers on the scroll bar, just 5 lines 1 in the beginning, 1 at the end, 1 at 25, 1 at 50, and 1 at 75 (When I hover my mouse over it, it says "Adjusts the execution speed").
It usually is set to the middle (at 50).
Nosson1459 Nosson1459

2016/12/8

#
That's default, but there are two ways it might not be that 1) the scenario creator adjusted it himself 2) it's a scenario taken from the Greenfoot book, and that got downloaded as being something other than 50.
danpost danpost

2016/12/8

#
Nosson1459 wrote...
That's default, but there are two ways it might not be that 1) the scenario creator adjusted it himself 2) it's a scenario taken from the Greenfoot book, and that got downloaded as being something other than 50.
Then run the scenario and move it to where the score increments about once every second again.
Nosson1459 Nosson1459

2016/12/8

#
This is all with these APS calculations, I of course like my own work better.
Nosson1459 Nosson1459

2016/12/9

#
Here is my Timer class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.text.*;

/**
 * This class should be added in to the world when needed, and will continue displaying the amount of seconds that elapsed since the addition to the world.
 * 
 * @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=0.0000; // this is what timer is equal
    private static final Color transparent = new Color(0,0,0,0);
    private int a=0;
    private String prefix;
    private GreenfootImage time;
    public Timer(String prefix)
    {
        this.prefix=prefix;
        updateTime();
    }
    /**
     * 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();
    }    
    /**
     * Update the image on screen to show seconds stopwatch
     */
    private void updateTime()
    {
        MyWorld world=(MyWorld)getWorld();
        if (a != 0)
        {
            stopTime=System.currentTimeMillis(); //gets current time in milliseconds
            elapsedTime=(stopTime-startTime)/1000.0; //converts time to seconds
            if (world.stopped==true)
            {
                startTime+=(world.startedTime-world.stoppedTime);
                world.stopped=false;
            }
        }
        time=new GreenfootImage(prefix + new DecimalFormat("0.0").format(elapsedTime), 22, Color.BLACK, transparent); // 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);
    }
}
In MyWorld I define these variables:
public long stoppedTime;
public long startedTime;
public boolean stopped=false;
Then I add the following two methods that automatically get called when the execution is stopped or started:
public void stopped()
{
    stopped=true;
    stoppedTime=System.currentTimeMillis();
}
public void started()
{
    if (stopped==true)
    {
        startedTime=System.currentTimeMillis();
    }
}
All this works fine. When the scenario is paused it won't count the paused time, the only problem is it's a little much just for a timer, but I like it, as I thought of all of the coding.
You need to login to post a reply.
1
2