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

2013/12/29

How to make multiple objects disappear?

chrl98 chrl98

2013/12/29

#
How do I make several objects disappear by detecting a click from another object? So for example I have a start menu with the buttons "Start", "Credits" and "Instructions", how do I program it so that when "Start" is clicked, "Credits" and "Instructions" disappear simultaneously as well? I have tried with the code below (using the removeObjects method) but to no avail.

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
import java.lang.Class;
import java.lang.Object;

public class Play extends MenuButtons
{
    private boolean Startgame = false;
    private boolean clicked = false;
    private GreenfootImage background;
    
    public Play() 
    {
        setImage (new GreenfootImage ("Start Game", 30, Color.WHITE, Color.BLACK));
        background = getImage();
        updateImage();
    }    
    public boolean getStart()
    {
       return Startgame;
    }
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
           Startgame = true;
           updateImage();
           getWorld().removeObject(this);
           //getWorld().removeObject((CreditsButton.class));
           //getWorld().removeObject((InstructionsButton.class));
           TerrainWorld w = getWorld();
           w.removeObjects(w.getObjects(CreditsButton.class));
           w.removeObjects(w.getObjects(InstructionsButton.class));
        }
        //List objects = getObjects(null);  
        //removeObjects(CreditsButton.class); 
    }    
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        setImage(image);
    }
}

Getting an error in line 32 "incompatible types". Thanks.
erdelf erdelf

2013/12/29

#
  
           World w = getWorld();    
           w.removeObjects(w.getObjects(CreditsButton.class).get(0));  
           w.removeObjects(w.getObjects(InstructionsButton.class).get(0));  
           w.removeObject(this)
replace line 29 to 34 with this should work
danpost danpost

2013/12/29

#
Change 'TerrainWorld' on line 32 to 'World'. 'getWorld()' returns a World object, not a TerrainWorld object; although we know it is one and can be typecast to one with '(TerrainWorld)getWorld()'. Then, move line 29 to after line 34 and change 'getWorld()' on that line with 'w'. If you remove 'this' from the world first, 'getWorld' will return a null value afterwards (see line 32, where null would be returned). .
chrl98 chrl98

2013/12/29

#
Thanks both. :)
You need to login to post a reply.