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

2016/7/12

Need help stopping a timer when the world is changed.

Exelious Exelious

2016/7/12

#
Hello, I'm trying to make my timer stop when the world is changed to the game over screen. The solution is probably very simple, but i just have no idea how to do it or where to start. Any help is greatly appreciated. For my timer.
public class TimerActor extends Actor
{
    private boolean running = false;
    private int millisElapsed = 0;
    private long lastTime = 0;
        
    /**
     * Act - do whatever the TimerActor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {            
        long time = System.currentTimeMillis();
        
        if(lastTime != 0) {
            long diff = time - lastTime;
            millisElapsed += diff;
        }
        lastTime = time;
        
        updateImage();
    }    
            
     public TimerActor() {
        updateImage();
    }
    
    public void start() {
        millisElapsed = 0;
        lastTime = 0;
    }
    
    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);
    }
}
Super_Hippo Super_Hippo

2016/7/12

#
Do you really change the world? Do you pass the TimerActor object to the new world? I am asking because if you change the world, the object isn't transferred automatically and couldn't continue to run.
Exelious Exelious

2016/7/12

#
Hello! Thanks for responding and sorry for the slow response, I'm in Australia. But yes, I pass the TimerActor into the game over screen via my main characters code, i'll paste that below.
public class Shark extends Actor
{
    //Below is all related to movement and gravity.
    double dy = 0;
    double g = 0.1;
    double BOOST_SPEED = -4;
    private int speed = 7;
    
    //Below is all related to animation.
    static final int CHG_RATE = 10; // adjust to suit
    int chgImgIn = 1; 
    int imgNum = 1;
    String[] images = { "Player.fw.png", "Player2.fw.png" };
       
    /**
     * Act - do whatever the Shark wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation( getX(), (int)(getY() + dy));
        dy = dy + g;
        
        rotation();
        movement();
        animation();
        atWorldEdge();
        
      
    }
    
    public void animation()
    {
        chgImgIn--;
        if (chgImgIn == 0)
        {
          chgImgIn = CHG_RATE; // reset countdown
          imgNum = (imgNum + 1) % 2; // this will alternate {0, 1, 0, 1...}
          setImage(images[imgNum]);
        }   
    }
    
    public void rotation()
    {
        if (dy < -2.6) 
            setRotation(-30);
        
        else if (dy < 3)
            setRotation(0);
            
        else if (dy > 4)
            setRotation(50);
        
        else if (dy > 30)
            setRotation(80);
    }
    
    public void movement()
    {               
        if (Greenfoot.mouseClicked(null)) {
            dy = BOOST_SPEED;
        }
    }
    
// This is where the timer is passed to the GameOver world.     
public void atWorldEdge()
    {
       TimerActor timeractor = (TimerActor) getWorld().getObjects(TimerActor.class).get(0); 
       int tx = timeractor.getX();
       int ty = timeractor.getY();
        
       if(getY()<10 ) {
            getWorld().removeObject(timeractor);
            World newWorld = new GameOver();
            Greenfoot.setWorld(newWorld);  
            newWorld.addObject(timeractor, 357, 233);
            
       }
       
 
       if(getY()>495 ) {
      
            getWorld().removeObject(timeractor);
            World newWorld = new GameOver();
            Greenfoot.setWorld(newWorld);  
            newWorld.addObject(timeractor, 357, 233);
            
       }
    }
}
danpost danpost

2016/7/13

#
The TimerActor class is not coded in such a way as to be able to stop a TimerActor object while it is in a world. Also, it is not coded in a way to be able to access the actual 'long' value of milliseconds elapsed. The only option then (without changing too much) is to draw the image of the object in the GameOver world instead of adding the TimeActor object itself in the world:
GreenfootImage timerImage = timeractor.getImage();
int x = 357-timerImage.getWidth()/2;
int y = 233-timerImage.getHeight()/2;
newWorld.getBackground().drawImage(timerImage, x, y);
Use this in place of line 76 and in place off line 86. Actually, you can combine the two 'if' block (lines 72 to 78 and line 81 to 88 using this
if (getY() < 10 || getY() > 495)
and lines 69 and 70 can be removed as the 'tx' and 'ty' variables declared on those lines are not being used anywhere.
Exelious Exelious

2016/7/13

#
Thank you so much Danpost, it works perfectly now! You are the real MVP, what would I ever do without you? <3
You need to login to post a reply.