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

2018/12/10

Adding a point to the score counter.

lorcis__ lorcis__

2018/12/10

#
Hi, I'm trying to add a score each time the car collects a point but it's not adding to the score counter. I've tried a lot of different commands but it always comes up as different errors. This is the code for removing the muffin The actors that I have right now is car (collects the muffin), Muffin is the score, and I have Counter as an actor too.
if(isTouching(Muffin.class))
    {
        removeTouching(Muffin.class);
               
        
    }        
danpost danpost

2018/12/10

#
lorcis__ wrote...
Hi, I'm trying to add a score each time the car collects a point but it's not adding to the score counter. I've tried a lot of different commands but it always comes up as different errors. This is the code for removing the muffin The actors that I have right now is car (collects the muffin), Muffin is the score, and I have Counter as an actor too. << Code Omitted >>
It would help for one to know where the score counter is and how it is created. However, if you only have one Counter object in the world, you can get a reference to it with:
Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0);
lorcis__ lorcis__

2018/12/10

#
danpost wrote...
lorcis__ wrote...
Hi, I'm trying to add a score each time the car collects a point but it's not adding to the score counter. I've tried a lot of different commands but it always comes up as different errors. This is the code for removing the muffin The actors that I have right now is car (collects the muffin), Muffin is the score, and I have Counter as an actor too. << Code Omitted >>
It would help for one to know where the score counter is and how it is created. However, if you only have one Counter object in the world, you can get a reference to it with:
Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0);
The score counter has been added in the world "capworld"
public class capworld extends World
{
    Counter counter = new Counter();
     /**
     * Constructor for objects of class capworld.
     * 
     */
    public capworld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 800, 1);
        outer_ring();
        inner_ring();
        addObject(new car(), 200,150);

        prepare();
    }

    public Counter getCounter()
    {
        return counter;
    }

    public void outer_ring()
    {   for (int loop=0;loop<=625;loop=loop+25)
        {
            addObject(new wall1(),100+loop,100);
            addObject(new wall1(),100+loop,745);
        }
        for (int loop=0;loop<=625;loop=loop+15)
        {
            addObject(new wall1(),100,115+loop);
            addObject(new wall1(),725,115+loop);
        }
    }

    public void inner_ring()
    {   for (int loop=50;loop<=425;loop=loop+25)
        {
            addObject(new wall1(),200+loop,250);
            addObject(new wall1(),200+loop,595);
        }
        for (int loop=50;loop<=365;loop=loop+15)
        {
            addObject(new wall1(),250,215+loop);
            addObject(new wall1(),625,215+loop);
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(counter, 100, 40);

        Muffin muffin = new Muffin();
        addObject(muffin,265,205);
        Muffin muffin2 = new Muffin();
        addObject(muffin2,672,149);
        Muffin muffin3 = new Muffin();
        addObject(muffin3,675,448);
        Muffin muffin4 = new Muffin();
        addObject(muffin4,536,128);
        Muffin muffin5 = new Muffin();
        addObject(muffin5,670,695);
        Muffin muffin6 = new Muffin();
        addObject(muffin6,178,682);

        wall1 wall1 = new wall1();
        addObject(wall1,449,609);
        wall1 wall12 = new wall1();
        addObject(wall12,449,623);
        wall1 wall13 = new wall1();
        addObject(wall13,450,639);
        wall13.setLocation(449,638);

        Bomb bomb = new Bomb();
        addObject(bomb,325,204);
        Bomb bomb2 = new Bomb();
        addObject(bomb2,604,145);
        Bomb bomb3 = new Bomb();
        addObject(bomb3,147,583);
        Bomb bomb4 = new Bomb();
        addObject(bomb4,248,686);
        Bomb bomb5 = new Bomb();
        addObject(bomb5,576,694);
        Muffin muffin7 = new Muffin();
        addObject(muffin7,150,426);
        Muffin muffin8 = new Muffin();
        addObject(muffin8,495,635);
    }
}



Thank you for your help, I really appreciate it :)
danpost danpost

2018/12/10

#
lorcis__ wrote...
The score counter has been added in the world "capworld" << Code Omitted >>
Okay -- as an alternative to the line previously given, you could use:
Counter counter = ((capworld)getWorld()).getCounter();
You need to login to post a reply.