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

2017/4/26

A counter that always goes up

BrianOK BrianOK

2017/4/26

#
For the final part of my game, i need a counter that always goes up until I die so kinda like a timer (000000) goes up 100 points a second.
Xmin_Terminator Xmin_Terminator

2017/4/26

#
I'm bad at greenfoot but I may be able to help you make a timer, though It will only add 60 roughly every second. Create a new actor called counter. put this code in it.
int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score,60, Color.GREEN, Color.YELLOW));
        addScore();
    } 
    
    public void addScore()
    {
        score++;
    }
Just put this code in the new actor you create for the score counter, it will display the text Score : and then the current score. If you want to change the name just go up into line 8 and change the "Score : " , which is in blue, to whatever you want to display. Also if you want to change the color of the text and background enter your color in CAPS after the dot like I have done. Greenfoot will only have a few amount of colors to choose from. If you get an error underlining Color.RED, or whatever your color is, go to the top and under the line
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
and code:
import java.awt.Color;
I hope I have helped, really I don't know much, so maybe get somebody like danpost who is super epic and a great help at greenfoot, for your greeefoot help. Danpost is great at greenfoot and has helped loads of people.
Yehuda Yehuda

2017/4/27

#
With the code that Xmin_Terminator gave you it adds one to the score each act cycle. I don't think either of use know exactly how many act cycles there are per second.
Xmin_Terminator wrote...
Also if you want to change the color of the text and background enter your color in CAPS after the dot like I have done. Greenfoot will only have a few amount of colors to choose from.
There are certain presets of colors in Greenfoot which are the same as the Java ones and can be found here
If you get an error underlining Color.RED, or whatever your color is, go to the top and under the line...
If you're using the Greenfoot Color class then you SHOULD NOT add in the import for java.awt.Color because everything in Greenfoot uses the greenfoot.Color class.
I hope I have helped, really I don't know much, so maybe get somebody like danpost who is super epic and a great help at greenfoot, for your greeefoot help. Danpost is great at greenfoot and has helped loads of people.
Am I allowed to help?! If you want to have your number increase by exactly 100 every second then you can use System.currentTimeMillis() to get the current time for starting, then in middle of the game you can get the amount of elapsed milliseconds by doing:
elapsedTime = (int) (System.currentTimeMillis() - startTime);
You should then divide that number by 10 to make it from milliseconds to 1/100th of a second (1000 / 10 = 100).
Xmin_Terminator Xmin_Terminator

2017/4/27

#
I said I don't know much about greenfoot, and I just said danpost because he has helped me a lot, just trying to share what I know, sorry for what I got wrong.
You need to login to post a reply.