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

2017/10/29

I am trying to create a Score Counter in my Game

proshiv proshiv

2017/10/29

#
In my game, everytime a bullet hits the asteroid, the score counter should increment by 1. As shown in this code, I add the score counter into my ending screen world:
public class EndingWorld extends World
{

    /**
     * Constructor for objects of class EndingWorld.
     * 
     */
    public EndingWorld()
    {    
        // Create a new world with 700x500 cells with a cell size of 1x1 pixels.
        super(700, 500, 1); 
        addObject(new EndingScreen(), 350, 250);
        addObject(new ScoreCount(), 100, 400);
    }
}
This is my score counter actor class:
public class ScoreCount extends AsteroidSubclass
{
    public ScoreCount()
    {
        setImage(new GreenfootImage("Score = " + scoreCount, 18, Color.BLACK, Color.WHITE));
    }
}
I have a method in my Asteroid subclass that destroys the small asteroid every time a bullet hits it
public void bulletDestroySmallAsteroid()
    {
    if (intersectObject(Bullet.class)) 
       {
        destroy(Bullet.class);
        getWorld().removeObject(this);
        }
     
    }
This method is then implemented into my subclass asteroid (because I have different types of asteroids):
public void act() 
    {
         
        move(-4);
        
        edgeEnd();        
        if(bulletDestroySmallAsteroid())
        {
            scoreCount = scoreCount + 1;
        }
        
    }    
I have incremented the score, but how do I transfer the value of this scoreCount to the scoreCount in my ScoreCount class? Right now my ScoreCount just shows 0 and is not incrementing. Any help would be appreciated, thanks!
danpost danpost

2017/10/29

#
It appears you have placed the 'scoreCount' field in the Asteroid class. As such, each asteroid created will have its own scoreCount integer and none could ever reach the value of 2 (each becomes 1 when the asteroid is destroyed). Obviously, then, you have misplaced where the 'scoreCounter' field can be. If it a game score, put it in your game World subclass; if each object that shoots bullets is to maintain a score of its own, then put it in the class of the objects that shoot the bullets.
You need to login to post a reply.