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

2023/11/13

My score counter doesn't add up but resets even though it's static

SergeantBoomie SergeantBoomie

2023/11/13

#
I have made a score counter for my game, but whenever I shoot an enemy the score resets instead of adding. Here is my code in MyWorld:
public static int score=0;

public void act()
{
    showText("Score: " + score,350,350);
}
and this is my code in the projectile:
MyWorld score;
int scoreUfo=0;
int scoreUfoRot=0;
int scoreUfoTel=0;

public void act()
    {
        if(getY()>1)
        {
            this.move();
            punkteAdd();
        }
        else
        {
            this.getWorld().removeObject(this);
        }   
    }
public void scoreAdd()
    {
        Actor Ufo;
        Ufo = this.getOneIntersectingObject(Ufo.class);
        if(Ufo != null)
        {
            scoreUfo=scoreUfo+10;
        }
        Actor UfoRot;
        UfoRot = this.getOneIntersectingObject(UfoRotating.class);
        if(UfoRot != null)
        {
            scoreUfoRot=scoreUfoRot+20;
        }
        Actor UfoTel;
        UfoTel = this.getOneIntersectingObject(UfoTeleport.class);
        if(UfoTel != null)
        {
            scoreUfoTel=scoreUfoTel+50;
        } score.score=scoreUfo+scoreUfoRot+scoreUfoTel;
    }
whenever I shoot the projectile and hit something, the code displays the amount of score it's supposed to, but when I shoot another projectile the score goes back down to 0, how do i fix this?
danpost danpost

2023/11/14

#
Each projectile starts with its int values at zero. Maybe you should not have the score displayed, or stored (for that matter), in the Projectile class. Seems line something the world should do -- keeping track of and displaying game stats.
SergeantBoomie SergeantBoomie

2023/11/14

#
I thought that it was already stored in the MyWorld class? How do I change it so the addScore method works in MyWorld?
danpost danpost

2023/11/16

#
SergeantBoomie wrote...
I thought that it was already stored in the MyWorld class? How do I change it so the addScore method works in MyWorld?
Yeah, but it is only getting the current projectile's scores. It is not combining the scores of all previous projectiles. Plus, it probably should not be a static field as one score field should suffice for one run of the scenario. Just move the score-related fields to the MyWorld class add a method to adjust the score in that class:
private int score, scoreUfo, scoreUfoRot, scoreUfoTel;

public void adjustScore(int which, int amount) {
    switch(which) {
        case 0: scoreUfo = scoreUfo+amount; break;
        case 1: scoreUfoRot = scoreUfoRot+amount; break;
        case 2: scoreUfoTel = scoreUfoTel+amount; break;
    }
    score = scoreUfo+scoreUfoRot+scoreUfoTel;
    showText("Score: "+score, 350, 350);
}
I do not think I can give you a decent projectile class coding. Problem is the intersecting objects are not being removed -- at least not from the projectile class. This could cause problems with the score keeping. If being remove, they may be removed before the projectile "sees" it; and if they are not being removed, the score will be increased multiple times (multiple act steps) while the intersection occurs. Q: Are the Ufo, UfoTeleport and UfoRotating objects being removed when touched by a projectile? Or, rather, should they be removed when found touching a projectile? Without removing here:
private void scoreAdd() {
    if (isTouching(Ufo.class)) {
        ((MyWorld)getWorld()).adjustScore(0, 10);
    }
    if (isTouching(UfoRotating.class)) {
        ((MyWorld)getWorld()).adjustScore(1, 20);
    }
    if (isTouching(UfoTeleport.class)) {
        ((MyWorld)getWorld()).adjustScore(2, 50);
    }
}
If removing ufos, just add to each if block:
removeTouching(Ufo.class);
(using appropriate class names)
You need to login to post a reply.