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

2012/5/2

How can you let an actor counting down

1
2
3
martijn13039 martijn13039

2012/5/2

#
I have a code so I can stop greenfoot after 60 seconds but is it posible that you see the actour counting down or something like that, I hope somebody could help me. martijn
import greenfoot.*;    

public class timer extends Actor   
{      
    long initialTime;   

  
    public Tijd()   
    {           
        initialTime = System.currentTimeMillis();      
    }  

    public void act()   
    {           
        if(System.currentTimeMillis() > initialTime + 60000)   
            ((BackGround) getWorld()).gameOver();  

    }        

}   
martijn13039 martijn13039

2012/5/2

#
If you don“t understand what I mean ask me some more questions and I hope you can explain me it after that
erdelf erdelf

2012/5/2

#
what do you exactly mean with
martijn13039 wrote...
is it posible that you see the actour counting down
? Do you want to see a string which counts down`?
PStigerID PStigerID

2012/5/2

#
In the World write ""public static int time = 0;" then change your code:
public Tijd()
{
     initialTime=System.currentTimeMillis();
     getWorld.addObject(new Counter(),x,y); // x and y position of counter - use getX and getY if nessesary
}

public void act()     
     {
          [NameOfWorld].time = initialTime-System.currentTimeMillis();

         if(System.currentTimeMillis() > initialTime + 60000)     
             ((BackGround) getWorld()).gameOver(); 
     }
Now you must create an actor which displays this int. This one should include:
import java.awt.Color;

public void act()   
{
    setImage(new GreenfootImage([NameOfWorld].time/1000, 20, Color.WHITE, Color.BLACK)); //bg = BLACK, char = white
}
Should work!
danpost danpost

2012/5/2

#
1) If you want to see the actor, you need to add it to the world 2) If it is to be added to the world, it is going to need an image 3) If the image is to show the time remaining, you need a variable to hold that data SO, you need to (reversing the order) 1) create a variable to hold the remaining time 2) create an image to display on the screen 3) make sure the world uses 'addObject' on this actor and 4) make sure the act method for this object updates the value of the variable and updates the image (it would probably be best to have a method 'updateImage()' that can be called from the act method, as well as the constructor).
erdelf erdelf

2012/5/2

#
@PStigerID, Oh he means a string which displays it :)
PStigerID PStigerID

2012/5/2

#
At least that's how I interpreted it ;)
danpost danpost

2012/5/2

#
I was thinking more in the lines of
import greenfoot.*;
import java.awt.Color;

public class Timer extends Actor // standard for class name is capitalizing first character
{
    long initialTime = System.currentTimeMillis();
    int elapsedTime = 0;

    public Timer()
    {
        updateImage();
    }

    public void act()
    {
        elapsedTime = System.currentTimeMillis() - initialTime;
        updateImage();
        if(elapsedTime >= 60000) ((BackGround) getWorld()).gameOver();
    }

    private void updateImage()
    {
        setImage(new GreenfootImage("" + ((60000 - elapsedTime) / 1000), 24, Color.black, new Color(0, 0, 0, 0));
    }
}
'new Color(0, 0, 0, 0)' is 'no color (transparent)'.
danpost danpost

2012/5/2

#
I took it as meaning that he had the Actor timer, but was not displaying it at all.
erdelf erdelf

2012/5/2

#
@danpost not sure, but is this code from your MillisecondTimer ?
danpost danpost

2012/5/2

#
I think in line 23 of my code you will need to cast the value to an int "" + ((int) ((60000 - elapsedTime) / 1000))
danpost danpost

2012/5/2

#
I do not think so, but I will look to see if it did come from there.
erdelf erdelf

2012/5/2

#
danpost wrote...
I do not think so, but I will look to see if it did come from there.
I used it sometimes and your code here reminds me of this code.
danpost danpost

2012/5/2

#
No, it is not from that scenario. But I believe I helped him out in another discussion dealing with a 60-second countdown timer.
martijn13039 martijn13039

2012/5/2

#
jeh PStigerID is right what I mean but I have the next problem if I compile there stay cannont find symbool variable getWorld
import greenfoot.*;
import java.awt.Color;    

public class Tijd extends Actor   
{      
    long initialTime;   

  
    public Tijd()   
    {           
        initialTime = System.currentTimeMillis();
        getWorld.addObject(new Counter(), 100, 100);
    }  

    public void act()   
    {    
        tijd.time = initialTime-System.currentTimeMillis();
        if(System.currentTimeMillis() > initialTime + 60000)   
            ((BackGround) getWorld()).gameOver();  
            
        setImage(new GreenfootImage
        (Tijd.time/1000, 20, Color.WHITE, Color.BLACK))

    }        

}   
There are more replies on the next page.
1
2
3