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

2020/9/10

Not able to create a score counter

Coder12341234 Coder12341234

2020/9/10

#
Hi, I am creating a space shooter game. I want add a score counter in my project but not able to do so. I am using the latest version. Following are the codes:- For the World:- import greenfoot.*; /** * Write a description of class Space here. * * @author (your name) * @version (a version number or a date) */ public class Space extends World { Counter counter = new Counter(); /** * Constructor for objects of class Space. * */ public Space() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 600, 1); prepare(); } public Counter getCounter() { return counter; } public void act() { addEnemy1(); addEnemy2(); } public void addEnemy1() { if(Greenfoot.getRandomNumber(120)<1) { addObject(new Enemy1(),Greenfoot.getRandomNumber(600), 0); } } public void addEnemy2() { if(Greenfoot.getRandomNumber(200)<1) { addObject(new Enemy2(),Greenfoot.getRandomNumber(600), 0); } } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Player player = new Player(); addObject(player, 311, 533); Counter counter = new Counter(); addObject(counter, 50, 50); counter.setLocation(80, 29); } } For the enemy:- import greenfoot.*; /** * Write a description of class Enemy1 here. * * @author (your name) * @version (a version number or a date) */ public class Enemy1 extends Enemy { /** * Act - do whatever the Enemy1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Enemy1() { setRotation(90); } public void act() { moveEnemy(); removeEnemy(); hitByProjectile(); } public void hitByProjectile() { Actor projectile = getOneIntersectingObject(Projectile.class); if (projectile != null) { Space space = (Space)getWorld(); space .removeObject(projectile); Counter counter = space.getCounter(); counter.addScore(); getWorld() .removeObject(this); } } } For the counter:- import greenfoot.*; /** * Write a description of class counter here. * * @author (your name) * @version (a version number or a date) */ public class Counter extends Actor { int Score = 0; /** * Act - do whatever the counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Counter() { setImage(new GreenfootImage("Score:" + Score , 50, Color.GREEN, Color.BLACK)); } public void act() { setImage(new GreenfootImage("Score:" + Score , 50, Color.GREEN, Color.BLACK)); } public void addScore() { Score++; } }
danpost danpost

2020/9/10

#
The problem is that you are creating two (2) counters; and the one in the world is not the one in the Counter counter field of your Space instance. Remove first instance of the string "Counter" from the prepare method. (actually, you can remove that line altogether)
Coder12341234 Coder12341234

2020/9/11

#
Thank you, Now it is working╰(*°▽°*)╯.
Coder12341234 Coder12341234

2020/9/11

#
import greenfoot.*;

/**
 * Write a description of class Enemy here.
 * @author (your name) @version (a version number or a date)
 */
public class Enemy extends Actor
{
    

    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        /* Write your action code here.*/
    }

    /**
     * 
     */
    public void moveEnemy()
    {
        setLocation(getX(), getY() + 3);
    }

    /**
     * 
     */
    public void removeEnemy()
    {
        if (getY() == 599) 
        {
            getWorld().removeObject(this);
        }
    }
}
When the enemy2 reaches the end of the world it show's an error.
Coder12341234 Coder12341234

2020/9/11

#
My world size is (600, 600, 1);
danpost danpost

2020/9/11

#
The problem is that you have two (2) methods that could potentially remove the actor from the world, you call them unconditionally and they both require that the actor be in the world when called. You could use an if statement to place the condition of the second calling that the actor be in the world, as such:
removeEnemy();
if (getWorld() != null) hitByProjectile();
However, if you decide to rearrange things, it may cause issues again. Better, would be to start both methods, removeEnemy and hitByProjectile, with the following line:
if (getWorld() == null) return;
You need to login to post a reply.