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

2019/3/2

Adding object to world from while other world is active

Zestix Zestix

2019/3/2

#
Hey I'd like to add an object to a world while another world is active. It doesn't seem to add it though, instead it adds it to the active world and removes it when I switch over to the other world Code:
int difficulty = 0;


if (isTouching(Medium.class))
        {
            TouchingM = true;
            if (Greenfoot.mousePressed(getWorld().getObjects(Medium.class).get(0)))
            {
                Greenfoot.setWorld(new PlantsVsZombies());
                difficulty = 2;
                getWorld().addObject(new Green(),500,500);
            }
        }
        else{
            TouchingM = false;
        }
nccb nccb

2019/3/2

#
Which world are you expecting line 11 to add to? getWorld() returns the world that the current actor is in, so it will not return the new PlantsVsZombies, it will return the existing world. If you want to add to the new world, keep a reference to it, then call addObject on that world reference rather than using getWorld()
Zestix Zestix

2019/3/2

#
nccb wrote...
Which world are you expecting line 11 to add to? getWorld() returns the world that the current actor is in, so it will not return the new PlantsVsZombies, it will return the existing world. If you want to add to the new world, keep a reference to it, then call addObject on that world reference rather than using getWorld()
How exactly do I do that?
You need to login to post a reply.