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

2017/4/26

How to implement counter in level 2?

ali18 ali18

2017/4/26

#
I have a counter that is functional on level 1, however, as i move onto the next level, the counter is still there, but it is not functional and a red message pops up. I have shown the code that i have used
public void remove()
    {
        Actor person = getOneObjectAtOffset(0, 0, person.class);
        if (person != null)
        {
            World myWorld = getWorld();
            getWorld().removeObject(person);
            level1 level1 = (level1) getWorld();
            Counter counter = level1.getCounter();
            counter.bumpCount(5);
            //Level1 level1 = (Level1)myWorld;
            //score score = level1.getsco();
            //counter.addPoints();
        }
    }
below:
danpost danpost

2017/4/26

#
Your message is probably something about not being able to convert Level2 to Level1 or incompatible types on line 8. There are two ways (at least) to get around this. One involves adding a superclass between World and your current level world classes and moving the Counter field and getter method into it. The other is to find the Counter object within the world instead of using the getter method in your level worlds. You can only have one Counter object in the world for this way to work. The remove method would be something like this:
public void remove()
{
    Actor person = getOneObjectAtOffset(0, 0, person.class);
    if (person != null)
    {
        getWorld().removeObject(person);
        ((Counter)getWorld().getObjects(Counter.class).get(0)).bumpCount(5);
    }
}
ali18 ali18

2017/4/26

#
Thank you for all your help, it is all functioning well.
You need to login to post a reply.