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

2013/5/10

how to pause a timer

MBX5 MBX5

2013/5/10

#
im trying to pause the timer (code below) when my meteor 'eats' my ship and everything i do the timer keeps going. is there any way to pause the timer in this way??
import greenfoot.*;
public class TimerActor extends Actor 
{
    
    private boolean running = false;
    private int millisElapsed = 0;
    private long lastTime = 0;
    
    public TimerActor() 
    {
        updateImage();
    }
    
    public void start() 
    {
        millisElapsed = 0;
        lastTime = 0;
    }
    
    public void gamePaused() 
    {
        lastTime = 0;
    }
    
    public void act() 
    {
        long time = System.currentTimeMillis();
        if(lastTime != 0) {
            long diff = time - lastTime;
            millisElapsed += diff;
        }
        lastTime = time;
        
        
        updateImage();
    }
    
    public void updateImage() 
    {
        // Calculate minutes & seconds elapsed
        int millis = millisElapsed % 1000;
        int secs = (millisElapsed / 1000) % 60;
        int mins = millisElapsed / 60000;
        // Convert these into text
        String millisText = String.format("%03d", millis);
        String secsText = String.format("%02d", secs);
        String minsText = "" + mins;
        String text = minsText + ":" + secsText + "." + millisText;
        // Update the image
        GreenfootImage img = new GreenfootImage(text, 25, null, null);
        setImage(img);        
       
    }    
    
}
danpost danpost

2013/5/10

#
Need to see the code where the meteor 'eats' the ship; plus the code creating the timer object and need to know where that code is located.
MBX5 MBX5

2013/5/10

#
Meteor Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Meteor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Meteor extends Animal
{
    /**
     * Act - do whatever the Meteor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean isShipEaten = false;
    
    public void act() 
    {
        move(7);
        tryToEatShip();
        randomTurn();
        turnAtEdge();
        
    }           
    public void randomTurn()
    {
        if ( Greenfoot.getRandomNumber(100) < 10 )
        {
        turn(Greenfoot.getRandomNumber(20) - 20);
        }
    }    
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {            
         turn(7);
        }
    }
    public void tryToEatShip()
    {
        if(canSee(Ship.class))
        {
            eat(Ship.class);
            isShipEaten = true;
        }
    }
   
}
danpost danpost

2013/5/10

#
And what and where is the code creating the timer object and adding it to the world?
MBX5 MBX5

2013/5/10

#
where would that be?
danpost danpost

2013/5/10

#
Probably in your sub-class of world, where you find "new TimerActor()".
MBX5 MBX5

2013/5/10

#
cant seem to find it
danpost danpost

2013/5/10

#
Do you see a timer object in your world (displayed)? You say the timer keeps going; I presume you can see it.
danpost danpost

2013/5/10

#
Either somewhere in your code, you have the text "new TimerActor()" or you are introducing the timer object into the world manually by right-clicking on the class icon and selecting "new TimerActor()" from the option-list. Which way is it?
SwaliG SwaliG

2013/5/10

#
Or you could just press space 7 times and on the last space hold it with control
Kartoffelbrot Kartoffelbrot

2013/5/10

#
Or you only implement a boolean variable called break. IF the game should be paused you set it to 'true'. annd the act-method of the timer will only work then, like:
public void act()
{
if(break==false)
{
//type in here, what the act mthod should do
}
//place a method here, which can set brak to false and true
}
You need to login to post a reply.