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

2012/6/27

add enemy after every 300 points scored

davemib123 davemib123

2012/6/27

#
Hi, how would I get an object to appear every 300 points scored? Currently I have:
public void addScore(int s)
    {
        scorecount=s+scorecount;
        score.setText("Score: "+scorecount); 
         if (scorecount == 300) {
            addMiniBoss();
         }
    }
but this only adds the miniboss at just 300, how would I get the boss to keep appearing after every 300 points have been accumulated?
danpost danpost

2012/6/27

#
Use the modulus operator (returns the remainder part of the division). Change line 5 to:
if (scorecount % 300 = 0) {
davemib123 davemib123

2012/6/27

#
didnt think of modulus operator, getting an error:
unexpected type
required: variable; found : value
davemib123 davemib123

2012/6/27

#
I have this declared at the top
public int scorecount=0;
not sure if that is causing the problem?
danpost danpost

2012/6/27

#
My bad. Need two equal signs for condition.
if (scorecount % 300 == 0) {
When you that particular error, usually this is why.
davemib123 davemib123

2012/6/27

#
works wonderful :)
limefortheworld limefortheworld

2012/6/28

#
On a sidenote, keep in mind that
if (scorecount % 300 == 0)
will work only if you have it so that score will pass through (i.e. land on) the multiples of 300. So jumping from 299 to 301 will not spawn the said miniboss, so this will only work if the score goes up only by some factor of 300 and only that factor. To mitigate this,
private int increment = 0;
public void addScore(int s)  
    {  
        scorecount=s+scorecount;  
        score.setText("Score: "+scorecount);   
         if (scorecount >= increment *300) {  
            addMiniBoss();  
            increment ++;
         }  
    }  
This will allow you to accomodate things that involve multiple scoring methods and values. But then, this is moot when the point earning is fixed to one way and one value. (e.g. killing enemy = 1 point and nothing else)
danpost danpost

2012/6/28

#
Another way is to use this for the addScore method:
public void addScore(int s)
{
    for (int i = 0; i < s; i++)
    {
        scorecount++;
        if (scorecount % 300 == 0) addMiniBoss();
    }
    score.setText("Score: " + scorecount);
}
You need to login to post a reply.