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

2022/5/28

We need help adding a counter to our game

MJ_lol MJ_lol

2022/5/28

#
Hey, we are two students and we´re currently creating a sort of space game, where you have to lead a rocket through meteors to the safe space-shuttle. Throughout the journey, there will be 3 stars (yes this is inspired by Mario lmao) and we want to add the counter for those three stars. Like you get the first star -> the counter says "Congrats, you´ve found 1 Star!" and then another and the 1 goes up to 2 and so on. We already tried some stuff but "private int score = 0" wont work and we don´t know why so we are looking for answers here :)
RcCookie RcCookie

2022/5/28

#
Can you show some code of what you tried with the int variable? Looks like the right approach
MJ_lol MJ_lol

2022/6/3

#
RcCookie wrote...
Can you show some code of what you tried with the int variable? Looks like the right approach
Sorry for not answering! We fixed our problem lol But if it makes u feel better: we have a new one! (ha ha ha) So if u could maybe help us with that lmao Basically, we want to add a picture which pops up after our rocket captured 3 stars and then make the picture disappear after a certain amount of time. We already tried looking here for a code or something but weren´t lucky lol
RcCookie RcCookie

2022/6/3

#
You should probably create a class for that. It takes the image of whatever the popup should show. To measure time, there are two main ways of doing it: - Counting frames - Calculating the time since the object was added Both ways are about the same complexity. Here's how to do it:
// Counting frames

// Class field
int remaining = 300;

// Inside act method
if(--remaining <= 0)
    getWorld().removeObject(this);
// Measuring time

// Class field
long end = System.currentTimeMillis() + 5000;

// Inside act method
if(System.currentTimeMillis() <= end)
    getWorld().removeObject(this);
At a speed of 50 both implementations should wait 5 seconds. The main difference is that when counting frames, the time changes of you change the speed slider or there is lag. So I would prefer the second option.
You need to login to post a reply.