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

2011/11/20

Acts per second? Displaying numbers on-screen?

1
2
Royalblue64 Royalblue64

2011/11/20

#
I'm trying to make a game where there is a timer tracking how long you can last. How many acts are in a second so that I could get it exactly right? And when I say how many per second I mean when the speed bar is directly in the center. Also, I don't know how to display the number onscreen. I tried the drawString method, but that didn't work because it's an int not a string. Any ideas on this so that I could have it show your score (time)?
kiarocks kiarocks

2011/11/20

#
One, it is way easier to use System.currentTimeMillis() to keep track of time. Set the start time like this:
int starttime = System.currentTimeMillis();
and in the act method do this:
if(System.currentTimeMillis() == starttime)
    {
        timepassed  = ((System.currentTimeMillis() - starttime) / 10); 
    }
Royalblue64 Royalblue64

2011/11/20

#
when I tried this it came up with an error :( this is the error: possible loss of precision found : longrequired: int any help with that would be great
mjrb4 mjrb4

2011/11/20

#
@kiarocks I'm not sure how the above approach is meant to work - to start with it'll complain as pointed out because you need a long not an int there, and your if statement in the act method would only ever return true (ignoring epochs!) less than a millisecond after the variable was initialised. You could initialise a field:
private long startTime = System.currentTimeMillis();
Then in your act method:
long msSinceLast = System.currentTimeMillis()-startTime;
startTime = System.currentTimeMillis();
The milliseconds since the last act method is in the msSinceLast variable - from this you should be able to work out how many act()s are roughly in a second. In terms of converting an integer to a string so you can display it, you can do this:
String str = Integer.toString(num);
kiarocks kiarocks

2011/11/21

#
sorry, still learning.
mjrb4 mjrb4

2011/11/21

#
Nothing wrong with still learning - to a certain extent we all are! But if you're not sure of code you've written then you should really test it before you post it, otherwise you may just confuse others.
Royalblue64 Royalblue64

2011/11/21

#
Thanks, I think that it's keeping time now, no real way to check yet, but I still can't put it on the screen yet. If anyone could advise me on that, it would be much appreciated.
kiarocks kiarocks

2011/11/22

#
mjrb4 wrote...
In terms of converting an integer to a string so you can display it, you can do this:
String str = Integer.toString(num);  
kiarocks kiarocks

2011/11/22

#
replace "num" with your int.
Royalblue64 Royalblue64

2011/11/23

#
could you do the same thing with longs but just String str = Long.toString(long);
darkmist255 darkmist255

2011/11/23

#
Try it out :D.
Royalblue64 Royalblue64

2011/11/23

#
Okay, ya it worked, but now there's a problem with putting it on the screen. Now when I try to do the drawString thing to make it draw the score into the world, the last score doesn't get erased and the new score overlaps it, making it a huge blob after two seconds D: Any help with making the score get erased then re-entered? I think that there's a clear() method somewhere in GreenfootImage, but I don't know how to use that... any other suggestions would be great. Also, with the current milliseconds thing, that doesn't quite work because it immediately goes up one and then stays at one instead of progressing to two and three and four, and so on.
darkmist255 darkmist255

2011/11/23

#
Try getWorld().removeObject(text.class)?
darkmist255 darkmist255

2011/11/23

#
right before adding the new text.class
Royalblue64 Royalblue64

2011/11/23

#
It won't compile, it says that text.class doesn't exist, so... I don't know. It's been done, of course, but I don't get it...
There are more replies on the next page.
1
2