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

2016/12/13

Removing object because another has been

Johnny567 Johnny567

2016/12/13

#
I'm trying to make it so that when my lives hit 0, my health bar goes away along with the other actors. However, I can only get my lives to disappear because I don't know how to bring an object from the world and remove it. Instead, I tried to make it so when the health bar disappears or the game is over, the ship and the counter disappear as well, but I can't make those into booleans so it's no use. Ship:
import greenfoot.*;
import java.awt.Color;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CopyOfShip here.
 * 
 *@Johnny567 
 * @version  1.0.0
 */
public class CopyOfShip extends Actor
{
    /**
     * Act - do whatever the CopyOfShip wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private GreenfootImage image1;
    private GreenfootImage image2;
    public void act() 
    {
    image1 = new GreenfootImage("rocket.png");
    image2 = new GreenfootImage("1446594018_medium.jpg");
    setImage(image1);

    if (Greenfoot.isKeyDown("d")) {
       setRotation(0); 
       setLocation(getX()+2, getY());
        }
    if (Greenfoot.isKeyDown("a")){
        setRotation(180);
        setLocation(getX()-2, getY());
    }    
    if (Greenfoot.isKeyDown("w")){ 
       setRotation(-90); 
       setLocation(getX(), getY()-2); 
    }
    if (Greenfoot.isKeyDown("s")){
       setRotation(90); 
       setLocation(getX(), getY()+2);
    }
    if (isTouching(Asteriod.class)) {
        setImage(image2);
        World myWorld = getWorld();
        MyWorld world = (MyWorld)myWorld;
        Lives lives = world.getLives();
        lives.loseLife();
    }
}
}
Health Bar:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Lives here.
 * 
 * @Johnny567
 * @version 1.0.0
 */
public class Lives extends Actor
{
    public int lives = 3;
    /**
     * Act - do whatever the Lives wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
     setImage(new GreenfootImage("Lives: " + lives, 24, Color.RED, Color.WHITE));
    if (lives == 0) {   
       World myWorld = getWorld();
       GameOver gameover = new GameOver();
       myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
       myWorld.removeObject(this);
    }
    }
    public void loseLife() {
        lives--;
    }
}
MyWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @Johnny567
 * @version 1.0.0
 */
public class MyWorld extends World
{
    Counter counter = new Counter();
    Lives lives = new Lives();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(600, 400, 1);
        addObject(lives, 35, 10);
        addObject(counter, 560, 10);
        CopyOfShip Ship = new CopyOfShip();
        addObject(Ship, 200, 300);
        Asteriod asteriod = new Asteriod();
        addObject(asteriod,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod2 = new Asteriod();
        addObject(asteriod2,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod3 = new Asteriod();
        addObject(asteriod3,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod4 = new Asteriod(); 
        addObject(asteriod4,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod5 = new Asteriod();
        addObject(asteriod5,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod6 = new Asteriod();
        addObject(asteriod6,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod7 = new Asteriod();
        addObject(asteriod7,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod8 = new Asteriod();
        addObject(asteriod8,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod9 = new Asteriod();
        addObject(asteriod9,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        Asteriod asteriod10 = new Asteriod();
        addObject(asteriod10,(Greenfoot.getRandomNumber(getWidth())),(Greenfoot.getRandomNumber(getHeight())));
        }
    public Counter getCounter() {
        return counter;
    }
    public Lives getLives() {
        return lives;
    }
       }
Counter: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Counter here. * * @Johnny567 * @version 1.0.0 */ public class Counter extends Actor { int score = 0; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(new GreenfootImage("Score: " + score, 24, Color.RED, Color.BLUE)); } public void addScore() { score++; } }
danpost danpost

2016/12/13

#
In your MyWorld class, add an act method that checks for all three objects. If any one is not in the world, remove them and stop the game from there (remove code that stops game elsewhere):
public void act()
{
    if (getObjects(CopyOfShip.class).isEmpty() || counter.getWorld() != this || lives.getWorld() != this)
    {
        removeObjects(getObjects(CopyOfShip.class));
        removeObject(counter);
        removeObject(lives);
        Greenfoot.stop();
    }
}
You need to login to post a reply.