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

2022/1/15

Using getWorld() method to remove Actors from World

Ronan444 Ronan444

2022/1/15

#
I am trying to remove 2 objects but when I attempt to use the getWorld() method it says "cannot find symbol" not sure why.
public int timer = 10; //Initialises the variable timer to 10

private void initialisation()
    {
        GameControlsDescription gamecontrolsdescription = new GameControlsDescription();
        addObject(gamecontrolsdescription, 600,350);
        addObject (new GameControlsDescription(), 600,350);
        
        TitleScreen titlescreen = new TitleScreen();
        addObject(titlescreen,600,350);
        addObject (new TitleScreen(), 600,350);
        //Will spawn in the GameControlsDescription and TitleScreen on top of each other in a specific order.
        //Due to the order the two are spawned in, the TitleScreen appears above it, so when the TitleScreen
        //is removed, it will show the Game Controls Description.
                
        timer--; //Will count down the timer
        if (timer <= 5) //When the timer is less than or equal to 5
        {
            //World world = getWorld();
            //removeObjects(getWorld().getObjects(TitleScreen.class));
            //getWorld().removeObject(TitleScreen.class);
            getWorld().removeObjects(getWorld().getObjects(TitleScreen.class));
        }
        if (timer <= 0) //When the timer is less than or equal to 0
        {
            //World world = getWorld();
            //removeObjects(getWorld().getObjects(GameControlsDescription.class));
            mainmenu(); //Calls the mainmenu method
            getWorld().removeObjects(getWorld().getObjects(GameControlsDescription.class));
        }
    }
danpost danpost

2022/1/15

#
The getWorld method is an Actor instance method. You cannot use it in a class that extends World. Use either "this." or nothing at all (just call the method "removeObjects").
Ronan444 Ronan444

2022/1/15

#
@danpost , thanks for replying again but I dont know if the timer in the code, do you think that works properly? I initlialised the timer to a value of 10, but is that 10 seconds in real time or number of cycles in the GreenFoot or something? Also I put in another line of code to check if the removeObject() method actually works and I dont think it does, have i done it correctly?
private void initialisation()
    {
        GameControlsDescription gamecontrolsdescription = new GameControlsDescription();
        addObject(gamecontrolsdescription, 600,350);
        addObject (new GameControlsDescription(), 600,350);
        
        TitleScreen titlescreen = new TitleScreen();
        addObject(titlescreen,600,350);
        addObject (new TitleScreen(), 600,350);
        //Will spawn in the GameControlsDescription and TitleScreen on top of each other in a specific order.
        //Due to the order the two are spawned in, the TitleScreen appears above it, so when the TitleScreen
        //is removed, it will show the Game Controls Description.
        
        if (Greenfoot.isKeyDown("p")) removeObjects(getObjects(TitleScreen.class));
        
        timer--; //Will count down the timer
        if (timer <= 5) //When the timer is less than or equal to 5
        {
            removeObjects(getObjects(TitleScreen.class)); //Will remove the TitleScreen object and show the GameControlsDescription
        }
        if (timer <= 0) //When the timer is less than or equal to 0
        {
            removeObjects(getObjects(GameControlsDescription.class)); //Will remove the GameControlsDecription
            mainmenu(); //Calls the mainmenu method
        }
    }
Ronan444 Ronan444

2022/1/15

#
Actually, never mind I managed to fix it, thanks alot for your help @danpost
You need to login to post a reply.