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

2021/11/18

Help im going insane about a Counter

1
2
Monk_08 Monk_08

2021/11/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Animal
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        counterSet();
        actCounter.setValue(actCounter.getValue() + points);
        move(7);
        die();
        if (getWorld() != null) kill();
    }
    
    int points = 0;
    private Counter actCounter;
    
    public void counterSet()
    {
        actCounter = new Counter("Act Cycles: ");
         getWorld().addObject(actCounter, 140, 50);
    }
    
    public void die()
    {
        if(isTouching(Bullet.class))
        {
            getWorld().removeObject(this);
            points++;
        }
    }
    
    public void kill()
    {
        if(isTouching(Robot.class))
        {
            getWorld().removeObject(this);
            //Greenfoot.stop();
            return;
        }
    }
}
My score counter only displays 0 and does not change why is it because I'm using if(isTouching) ?
Monk_08 Monk_08

2021/11/18

#
please ignore that it is called act counter its just a testing scenario
danpost danpost

2021/11/18

#
Every act step, except the first, you are replacing the counter with a new one. Use the addedToWorld method.
Monk_08 Monk_08

2021/11/19

#
protected void addedToWorld(World world)  
{  
    //my Counter   
}
Like this ? And when is it called every time the actor is added to the world right ?
Monk_08 Monk_08

2021/11/19

#
So I tested it and every time the rocket gets added it sets the counter that's correct but my problem is I'm adding the rocket multiple times so the counter is still resetting itself
danpost danpost

2021/11/19

#
Monk_08 wrote...
So I tested it and every time the rocket gets added it sets the counter that's correct but my problem is I'm adding the rocket multiple times so the counter is still resetting itself
Use:
protected void addedToWorld(World world)
{
    if (actCounter == null) actCounter = new Counter("Act Cycles: ");
    world.addObject(actCounter, 140, 50);
}
Monk_08 Monk_08

2021/11/20

#
Danpost thanks for the help appreciate it <3
Monk_08 Monk_08

2021/11/20

#
ok there is still a little problem
Monk_08 Monk_08

2021/11/20

#
The counters start layering on each other so when my score is 50 i have 50 counters on top of each other
danpost danpost

2021/11/20

#
Monk_08 wrote...
The counters start layering on each other so when my score is 50 i have 50 counters on top of each other
That is not from what I provided. It may be that you did not remove the codes that are to be replaced by what I gave. Show your revised class codes.
Monk_08 Monk_08

2021/11/20

#
I can’t find anything why it should
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Animal
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(7);
        die();
        System.out.println(("Points:") +money ); //to check if my counter is displaying the correct things 
        if (getWorld() != null) 
        {
            kill(); 
        }
    }

    protected void addedToWorld(World world)
    {
        if (moneyCounter == null) 
        {moneyCounter = new Counter("Money: ");
        world.addObject(moneyCounter, 60, 40);
        }
        moneyCounter.setValue(moneyCounter.getValue() + money);
    }

    static int money;
    public Counter moneyCounter;

    public void die()
    {
        if(isTouching(Bullet.class))
        {

            money++;
            getWorld().removeObject(this);

        }
    }

    public void kill()
    {
        if(isTouching(Robot.class))
        {
            getWorld().removeObject(this);
            //Greenfoot.stop();
        }
    }
}
danpost danpost

2021/11/20

#
Maybe you are (re-)adding a different Rocket object into the world instead of the same one. Show your World subclass codes.
Monk_08 Monk_08

2021/11/20

#
I directly wrote into MyWorld is that a problem?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 900, 1); 
        prepare();
        rocketSpawn(1);
    }
    
    public void act()
    {
        regulateRockets();
    }
    
    public void regulateRockets()
    {
        if (getObjects(Rocket.class).size() < 1)
        {
           rocketSpawn(1);
        }
    }

        public void prepare()
    {
        Robot robot = new Robot();
        addObject(robot , 450 ,450 );
    }
    
    public void rocketSpawn (int howMany)
    {
        for(int i=0; i<howMany; i++) {
            int border = Greenfoot.getRandomNumber(4);
            int position = Greenfoot.getRandomNumber(900);
            Rocket r = new Rocket();
            if(border == 1)
            {
                addObject(r ,position ,0);
            }else if(border == 2)
            {
                addObject(r ,0 ,position);
            }else if(border == 3)
            {
                addObject(r ,position ,900);
            }else if(border == 4)
            {
                addObject(r ,900 ,position);
            }
            r.turnTowards(450, 450);
        }
    }
}
danpost danpost

2021/11/20

#
Move line 48 to outside the method.
danpost danpost

2021/11/20

#
Is there ever a chance that more than one Rocket object is in the world at the same time? If so, things will need to be re-worked throughout. If not, you can simplify the code a bit.
There are more replies on the next page.
1
2