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

2012/5/14

Remove 2 other actors when 1 is selected.

TechWebLock TechWebLock

2012/5/14

#
Hi, I am trying to create a simple menu system that allows a user to select a level in my game. I have got this to work by creating a public variable called level in the world and then changing the number when the user presses a button. From here populate is run and IF functions set up the games level - it may not be the most optomised way but it seems to be holding up. The problem I am having now is actually removing the other actors. The actor I click on is easy but as for the others I have never been told how to remove them. I am used to getOneObjectAtOffset and ofcourse this wont work. Below is the code for the menu.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Easy level menu selection
 * 
 * @author (Connor) 
 * @version (1)
 */
public class level1select extends Actor
{
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            mainwindow fighterWorld = (mainwindow)getWorld();// create a specific world name for mainwindow
            fighterWorld.level = 1; //level selected is 1 (easy)
            fighterWorld.populate(); //add objects to game
//remove the other menus here
            fighterWorld.removeObject (this);//remove the menu
        }
}
}
Where the //remove other menus here is where I want to remove the other two menus level2select level3select I know I need a class to define that single actor but I havent any clue on how to get this to work. Thanks in advance, Connor
TechWebLock TechWebLock

2012/5/14

#
Also thrown in is the world code here
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * This section controls the level selection and also populates for easy.
 * 
 * @author (Connor Willer) 
 * @version (1)
 */
public class mainwindow extends World
{
    public int weaponPower = 0; //creates the initial weapon power
    public int powerup = 0; //sets value of powerup execution to 0 (3 activates a powerup)
    public int epd = 0; //sets the number of destroyed enemy planes to 0
    public int armor = 2; //sets the good planes armor to 2
    public int level = 1; //sets the level to easy
    private scoreboard scoreBoard; //creates the scoreboard variable
    private armor Armor; //creates the scoreboard variable
    public mainwindow()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 500, 1); 
        menuPopulate();
        scoreBoard = new scoreboard (); //defines the actor scoreboard as scoreboard
        addObject (scoreBoard, 300, 20); //positions the scoreboard
        scoreBoard.updateStatus (epd); //updates the scoreboard enemy planes destroyed
        Armor = new armor (); //defines the actor Armor as armor
        addObject (Armor, 600, 20); //positions the armor actor
        Armor.updateStatus (armor); //updates the armor for initial load
    }
    void menuPopulate()
    {
       addObject(new level1select(), 650, 100);
       addObject(new level2select(), 650, 250);
       addObject(new level3select(), 650, 400);
    }
    void populate() //add objects to the game
    {
        for(int i=0;i<500;i++)  //add borders down left side
            addObject(new border_b(), 0, i); 
        for(int i=0;i<1000;i++)  //add borders across the top
            addObject(new border(), i, 0);
        for(int i=0;i<500;i++)  //add borders down right side
            addObject(new border(), 1000, i);
        for(int i=0;i<1000;i++)  //down the bottom but keeps area for gate open
            addObject(new border(), i, 500);
        //adds good plane
        addObject(new gp1(), 150, 250);
        //add 1 bad plane
        addObject(new bp1(), 900,250);
        //add 1 bad plane
        addObject(new bp1(), 900,75);
        //add 1 bad plane
        addObject(new bp1(), 900,425);
    }
    public void increasePowerup ()
    {
        powerup = powerup + 1; ;//adds 1 to powerup
    }
    public void increaseWeaponNumber ()
    {
        weaponPower = weaponPower + 1; //adds 1 to powerup
    }
    public void increaseDestroyedEnemyPlane ()
    {
        epd = epd + 1; //adds one to  number of enemy planes destroyed
        scoreBoard.updateStatus (epd); //update score
    }
    public void damageArmor ()
    {
        armor = armor - 1; // removes 1 from the armor
        Armor.updateStatus (armor); //update armor
    }
    public void increaseArmor ()
    {
        if (armor == 9)
        {
            //do nothing
            Armor.updateStatus (armor);
        }
        else
        {
        armor = armor + 1; // adds 1 to the armor
        Armor.updateStatus (armor); //update armor
        }
    }
    public void gameOver()
    {
        addObject(new gameover(epd), 500,250); //loads the background image at 500, 250
    }
}
trash1000 trash1000

2012/5/14

#
I wouldn't delete the remaining menu objects from a menu object but from the world. I only scanned through your source code but you call populate() when the menu object is clicked. So you could put
public void populate() {
    removeObjects(getObjects(null));
    [...]
}
to remove any object that is in the world at method call. On a side note. Do you really use 3 or more different classes for your level selection? You could have only one class LevelSelect and pass over the level int in the constructor and store it in an instance variable.
public class LevelSelect extends Actor  
{  
    private int lvl;

    public LevelSelect(int lvl) {
        this.lvl = lvl;
    }
 
    public void act()  
    {  
        if (Greenfoot.mouseClicked(this))  
        {  
            mainwindow fighterWorld = (mainwindow)getWorld();
            fighterWorld.level = lvl;
            fighterWorld.populate();
        }  
}  
}  
TechWebLock TechWebLock

2012/5/14

#
That works perfectly, just had to shuffle the scores around so they would load. Thanks for that. As for the level classes I try and keep everything as un-complicated as possible then go back at the end and add in different more optimised codes. I will be using this when I have finished the last pieces of the game. Big thanks again :)
You need to login to post a reply.