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

2013/12/24

clock actor

zwartepiet333 zwartepiet333

2013/12/24

#
Hello, I got this scenario from Gevater_Tod4711 http://www.greenfoot.org/scenarios/7243, and I got a question about this clock. I adjusted it to only 1 timer, and the code looks like this now.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Clock here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class Clock extends Actor
{
    private int startingTime;
    
    private int seconds = 180;
    private int clockType = 0;
    
    private long lastCurrentSecond;
    private long timeSaved = 0;
    
    private boolean timeUp = false;
    private boolean count = false;
    private boolean displayTime;
    private boolean countDown;
    
    private String text;
    
    private Actor alertedActor;
    
    private World alertedWorld;
    
    /**
     * Create a new Clock with your own text or other values.
     * 
     * @param countDown
     *      Set the kind of the clock. If true the clock will cound down from the given starting time. Otherwise it'll count up from 0.
     * 
     * @param displayTime
     *      Set the clock's visibility. If true the clock is visible.
     * 
     * @param startingTime
     *      The starting time for countdown clocks. If you restart the clock it'll start from this value.
     * 
     * @param text
     *      The text above the clock. If text is null the clock will be centered and there is no text.
     */
    public Clock(boolean countDown, boolean displayTime, int startingTime, String text) {
        this.countDown = countDown;
        this.displayTime = displayTime;
        this.startingTime = startingTime;
        this.text = text;
        this.alertedActor = null;
        this.alertedWorld = null;
        seconds = startingTime;
        getImage().clear();
        if (!countDown) {
            seconds = 0;
        }
        if (displayTime) {
            getImage().scale(130, 70);
        }
        this.startClock();
    }
  
    /**
     * The act method of the class Clock.
     * Let the clock count the time. If the act mehtod is not executed the clock will count on but not change it's image. To pause the clock use the method stopClock().
     * The Clock is irrespective of the acting speed of the scenario so that it doesn't mater how fast the other act methods are executed.
     */
    public void act() {
        if (countDown) {
            if (count && !timeUp) {
                if (System.currentTimeMillis() - lastCurrentSecond >= 1000) {
                    lastCurrentSecond += 1000;
                    seconds--;
                    if (displayTime) {
                        drawTime();
                    }
                }
                if (seconds == 0) {
                    //alertActor();
                    alertWorld();
                    timeUp = true;
                }
            }
        }
        else {
            if (count) {
                if (System.currentTimeMillis() - lastCurrentSecond >= 1000) {
                    lastCurrentSecond += 1000;
                    seconds++;
                    if (displayTime) {
                        drawTime();
                    }
                }
            }
        }
    }
    
    /**
     * Alert the actor you want.
     * You have to change this method to alert an other actor that the one in this demo. To alert another actor you should change the strings "Exampla_Actor" to the classname of the actor you want 
     * to alert and perhaps change the name of the called method.
     */
    //private void alertActor() {
       // if (alertedActor != null && alertedActor instanceof Example_Actor) {
         //   ((Example_Actor) alertedActor).alert();
        //}
    //}
    /**
     * Alert the world you want.
     * You have to change this method to alert an other world that the one in this demo. To alert another world you should change the strings "Clock_World" to the classname of the world you want 
     * to alert and perhaps change the name of the called method.
     */
    private void alertWorld() {
        if (alertedWorld != null && alertedWorld instanceof Clock_World) {
            ((Clock_World) alertedWorld).alert();
        }
    }
    
    /**
     * The drawing method of the clock.
     * This method draws the current value of your clock onto the clock object.
     */
    private void drawTime() {
        int min = (int) (seconds / 60);
        int sec = seconds % 60;
        String remainingTime;
        if (sec < 10) {
            remainingTime = min + ":0" + sec;
        }
        else {
            remainingTime = min + ":" + sec;
        }
        getImage().setColor(Color.gray);
        getImage().fill();
        GreenfootImage text = new GreenfootImage((this.text == null ? "" : this.text), 30, Color.black, new Color(0, 0, 0, 0));
        GreenfootImage time = new GreenfootImage(remainingTime, 40, Color.black, new Color(0, 0, 0, 0));
        if (text.getWidth() > getImage().getWidth()) {
            getImage().clear();
            getImage().scale(text.getWidth() + 10, 70);
            getImage().setColor(Color.gray);
            getImage().fill();
        }
        getImage().drawImage(text, (getImage().getWidth()/2)-(text.getWidth()/2), 5);
        getImage().drawImage(time, (getImage().getWidth()/2)-(time.getWidth()/2), (this.text == null ? (getImage().getHeight()/2)-(time.getHeight()/2) : 30));
    }
    
    /**
     * Check whether the time is up.
     * 
     * @return
     *      Returns true if the time is up. If the clock is no countdown clock the method will return false.
     */
    public boolean timeUp() {
        return timeUp;
    }
    
    /**
     * Start the clock.
     */
    public void startClock() {
        lastCurrentSecond = System.currentTimeMillis() - timeSaved;
        count = true;
    }
    /**
     * Pause the clock.
     */
    public void stopClock() {
        timeSaved = System.currentTimeMillis() - lastCurrentSecond;
        count = false;
    }
    /**
     * Reset the clock.
     */
    public void resetClock() {
        seconds = startingTime;
        timeUp = false;
    }
}


When i pause the clock, and wait 10 seconds. It doesnt continue from the moment when i paused the clock. Could anyone tell me how i can solve this?
danpost danpost

2013/12/24

#
Change these two methods as follows:
public void startClock()
{
    lastCurrentSecond = System.currentTimeMillis();
    count = true;
}

public void stopClock()
{
    timeSaved = 0;
    count = false;
}
zwartepiet333 zwartepiet333

2013/12/24

#
I changed both the startclock and stopclock method, but i still get the problem where it skips the seconds where it should be paused.
danpost danpost

2013/12/24

#
You need to call these methods when starting and stopping the clock. BTW, I found it is not precise either. When stopped, any portion of a second not completed is lost. I had wrote a Millisecond Timer scenario which might be more what you are looking for.
zwartepiet333 zwartepiet333

2013/12/24

#
In the Milisecond timer scenario, theres the same problem that i dont want. if i pause the scenario it still loses the seconds i paused when i play the scneario again. and where should i call these methods? i called them in the act, but the timer stops immediatly after i tried to run it.
danpost danpost

2013/12/24

#
After reviewing it, I realize that it would have the same problem, in that it does not hold when stopped. Let me ask you this, is it important that the timer be regulated by the system clock or can it be regulated by the number of act cycles ran? Using the system time will not always result in a consistent game-time; using act cycles, it will always be consistent. An added plus is that when the scenario is stopped, no act cycles are executed to be counted (or added to the 'timer').
zwartepiet333 zwartepiet333

2013/12/24

#
I dont think it would be necessary that it its bound to the system clock. As long as the time is holded if the game is paused. And if like you say, no act cycles are executed in the moment i pause the game, i think that way will be the better approach.
danpost danpost

2013/12/24

#
You will need the following: (1) an Actor object with an int field to hold the time units (one unit being a specified number of act cycles) that have passed that displays the elapsed time; (2) an int field to count the act cycles as they pass; The act counter should be incremented from within the act method, and each time it reaches some value around 50 to 60 or so, reset it to zero and increment the displayed value of the actor.
Aaron91 Aaron91

2013/12/29

#
Is it also possible to make a timer without it telling the minutes, so seconds only? so I can make a counter for a different metric system. As for my project, I need to have a counter that counts down from 540. But if I did that with time it would look like 9:00 if I am correct.
danpost danpost

2013/12/29

#
That would be so much easier, as you only need one timer field. Also, programming it to display minutes AND seconds is more complicated than just displaying the seconds. It is all in how you make the image show the value(s).
Aaron91 Aaron91

2013/12/29

#
Could you explain the timer field? And if I want to use meters instead of seconds, can i increase the speed of the count down?
danpost danpost

2013/12/29

#
Let us break it down to the necessary components. You need the following: (1) an actor object to display changeable text (2) a reference to this object with two int fields (timer and counter) The timer field will count act cycles until a pre-determined value is reached, at which point it is reset to zero and the counter field is incremented (or decremented) and the displayed text is changed. If the counter field increments, check for any target value after the change; and if being decremented, check for zero before the change and process any actions necessary then.
A\aron91 A\aron91

2013/12/30

#
i got these 2 actors. One "TimeChecker"
import greenfoot.*;  
public class TimeChecker extends Actor {  
    long initialTime;  
    public TimeChecker() {  
        initialTime = System.currentTimeMillis();  
    }  
      
    public void act() {  
        if(System.currentTimeMillis() > initialTime + 180000) {  
            //Put your code here that you want to happen when the game ends  
            Greenfoot.stop();  
            
        }  
    }  
}  
And the "Clock" actor.
 * @author (your name)
 * @version (a version number or a date)
 */
public class Clock extends Actor
{
    private int startingTime;
    
    private int seconds = 180;
    private int clockType = 0;
    
    private long lastCurrentSecond;
    private long timeSaved = 0;
    
    private boolean timeUp = false;
    private boolean count = false;
    private boolean displayTime;
    private boolean countDown;
    
    private String text;
    
    private Actor alertedActor;
    
    private World alertedWorld;
    
    /**
     * Create a new Clock with your own text or other values.
     * 
     * @param countDown
     *      Set the kind of the clock. If true the clock will cound down from the given starting time. Otherwise it'll count up from 0.
     * 
     * @param displayTime
     *      Set the clock's visibility. If true the clock is visible.
     * 
     * @param startingTime
     *      The starting time for countdown clocks. If you restart the clock it'll start from this value.
     * 
     * @param text
     *      The text above the clock. If text is null the clock will be centered and there is no text.
     */
    public Clock(boolean countDown, boolean displayTime, int startingTime, String text) {
        this.countDown = countDown;
        this.displayTime = displayTime;
        this.startingTime = startingTime;
        this.text = text;
        this.alertedActor = null;
        this.alertedWorld = null;
        seconds = startingTime;
        getImage().clear();
        if (!countDown) {
            seconds = 0;
        }
        if (displayTime) {
            getImage().scale(130, 70);
        }
        this.startClock();
    }
  
    /**
     * The act method of the class Clock.
     * Let the clock count the time. If the act mehtod is not executed the clock will count on but not change it's image. To pause the clock use the method stopClock().
     * The Clock is irrespective of the acting speed of the scenario so that it doesn't mater how fast the other act methods are executed.
     */
    public void act() {
        if (countDown) {
            if (count && !timeUp) {
                if (System.currentTimeMillis() - lastCurrentSecond >= 1000) {
                    lastCurrentSecond += 1000;
                    seconds--;
                    if (displayTime) {
                        drawTime();
                    }
                }
                if (seconds == 0) {
                    //alertActor();
                    //alertWorld();
                    timeUp = true;
                }
            }
        }
        else {
            if (count) {
                if (System.currentTimeMillis() - lastCurrentSecond >= 1000) {
                    lastCurrentSecond += 1000;
                    seconds++;
                    if (displayTime) {
                        drawTime();
                    }
                }
            }
        }
        
       
        
       
        
    }

    /**
     * The drawing method of the clock.
     * This method draws the current value of your clock onto the clock object.
     */
    private void drawTime() {
        //int min = (int) (seconds / 60);
        int sec = seconds;
        String remainingTime;
        if (sec < 10) {
            remainingTime = "0" + sec;
        }
        else {
            remainingTime = " " + sec;
        }
        getImage().setColor(Color.gray);
        getImage().fill();
        GreenfootImage text = new GreenfootImage((this.text == null ? "" : this.text), 30, Color.black, new Color(0, 0, 0, 0));
        GreenfootImage time = new GreenfootImage(remainingTime, 40, Color.black, new Color(0, 0, 0, 0));
        if (text.getWidth() > getImage().getWidth()) {
            getImage().clear();
            getImage().scale(text.getWidth() + 10, 70);
            getImage().setColor(Color.gray);
            getImage().fill();
        }
        getImage().drawImage(text, (getImage().getWidth()/2)-(text.getWidth()/2), 5);
        getImage().drawImage(time, (getImage().getWidth()/2)-(time.getWidth()/2), (this.text == null ? (getImage().getHeight()/2)-(time.getHeight()/2) : 30));
    }
    
    /**
     * Check whether the time is up.
     * 
     * @return
     *      Returns true if the time is up. If the clock is no countdown clock the method will return false.
     */
    public boolean timeUp() {
        return timeUp;
    }
    
    /**
     * Start the clock.
     */
    public void startClock() {  
        lastCurrentSecond = System.currentTimeMillis() - timeSaved;  
        count = true;  
    }
    /**
     * Pause the clock.
     */
    public void stopClock() {  
        timeSaved = System.currentTimeMillis() - lastCurrentSecond;  
        count = false; 
    }
    /**
     * Reset the clock.
     */
    public void resetClock() {
        seconds = startingTime;
        timeUp = false;
    }
    
    
    long initialTime;  
    public void TimeChecker() {  
        initialTime = System.currentTimeMillis();  
    }  
      
    }
And it looks like this in my World
  public BotenUpgrade()
    {    
      
        addTime();
        
       addObject(new Clock(true, true, 180, "Meter over:"), 700, 50);
        //addObject(new TimeChecker(true, true, 5000, "Meters over:"), 650, 50);
       
    }
    
private void addTime()
    {
        TimeChecker time = new TimeChecker();
        addObject(time, 700, 50);
        
    }
The thing is that i want the to let my boat to sail 5000 meters in 3 minutes. But it only does 1 meter each second. Could you tell me how i could do this?
danpost danpost

2013/12/31

#
Your Clock actor only registers seconds (not partial seconds). It would take a lot of work to fix this to what you want. Better would be to create your own timer system to suit your needs. Also, your TimeChecker class is really a waste of an actor and of a class. What it does should be done in your world class act method or a method that act method calls.
You need to login to post a reply.