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

#
oow sorry danpost i didn“t say you last post I wil check it out en lookt of it works
erdelf erdelf

2012/5/2

#
change line 12 to this:
        getWorld().addObject(new Counter(), 100, 100);
danpost danpost

2012/5/2

#
@erdelf , 'getWorld()' will still produce an error; as you cannot get the World from an object that is NOT in the world (yet; as that code is in the constructor). Also, as far as where the code came from. Yes, I was involved in the discussion I had mentioned, but the code was not supplied by me (Duta supplied it).
erdelf erdelf

2012/5/2

#
@danpost, sry I only corrected the line that insults the error. Change line 11 and 12
PStigerID PStigerID

2012/5/2

#
 getWorld().addObject(new Counter(), 100, 100);  
danpost danpost

2012/5/2

#
You know, I do not even know why a Counter object is even created for this scenario. The system timer IS the counter and is ALWAYS present.
PStigerID PStigerID

2012/5/2

#
But he wants this time displayed...
danpost danpost

2012/5/2

#
@PStigerID (and @erdelf) , You CANNOT use 'getWorld()' AT ALL in the constructor of an actor (not in reference to the actor that is being created). The object being created is NOT in the world and 'getWorld()' will produce NULL. A NullPointerException WILL ensue.
martijn13039 martijn13039

2012/5/2

#
danpost I have try your option but there is a problem there stay on elapsedTime = System.currentTimeMillis() - initialTime; on initialTime an red squar with possible loss of precision required: int; found; long have you an idee
import greenfoot.*;
import java.awt.Color;    

public class Timer extends Actor   
{      
    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("" + ((int) ((60000 - 
                            elapsedTime)) / 1000), 24, Colour.BLACK, new Color(0, 0, 0, 0)));

    }
}   

danpost danpost

2012/5/2

#
@PSigerID, he already has an object created, in the Tijd class (which is his timer class), but did not have it being added to the world, nor had an image for it. The code I gave on page 1 for the timer class gives it an image to display. All he need do now is addObject(new Tijd(), x, y) in the world.
danpost danpost

2012/5/2

#
sry, in Line 7, make elapsed time a 'long' instead of 'int'
martijn13039 martijn13039

2012/5/2

#
now I got this but er is some error with '';'' this sign long initialTime = System.currentTimeMillis(); long instead of int = 0;
danpost danpost

2012/5/2

#
NOT: long instead of int = 0; USE: long elapsedTime = 0;
martijn13039 martijn13039

2012/5/2

#
Danpost I'm sorry for that stupid question I really shame me but wil thank you and the other guys a lot it is working now I hope you wil forgive me :p
danpost danpost

2012/5/2

#
Just glad to know you are making progress.
There are more replies on the next page.
1
2
3