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

2020/2/14

How to reset the food when the eater was died?

Rafiqi_Fariz Rafiqi_Fariz

2020/2/14

#
i want the Lobster recreating again in same position after the Bango class was died/lifes-1. how to do it?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bango here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bango extends Actor
{
    /**
     * Act - do whatever the Bango wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int lifes = 3;
    private int hp;
    private int timerhealthbar;
    private HealthBar healthbar = new HealthBar();
    public void act() 
    {   
        getWorld().addObject(healthbar, getX(), getY());
        healthbar.setLocation(getX(), getY());
        checkLobster();
        checkCrocodile();
        movement();
        getWorld().showText("Life: "+lifes, 50,20);
    }
    
    public void movement(){
        if(Greenfoot.isKeyDown("up")){
            setRotation(270);
            move(2);
        }
        if(Greenfoot.isKeyDown("Down")){
            setRotation(90);
            move(2);
        }
        if(Greenfoot.isKeyDown("Left")){
            setRotation(180);
            move(2);
        }
        if(Greenfoot.isKeyDown("Right")){
            setRotation(0);
            move(2);
        }
    }
    
    public void testEndGame(){
        if(lifes <= 0){
            Greenfoot.stop();
        }
    } 
    
    public void resetPosition(){
        this.setLocation(48,60);
        healthbar.setLocation(getX(), getY());
        this.hp=0;
        healthbar.drawBar(hp);
    }
    
    public void removeLife(){
        lifes--;
        testEndGame();
        resetPosition();
    }
    public void checkLobster(){
        if(isTouching(Lobster.class)){
            removeTouching(Lobster.class);
        }
    }
    
    public void checkCrocodile(){
        if(isTouching(Crocodile.class)){
            hp++;
            healthbar.drawBar(hp);
            if(hp == 100){
                removeLife();
                hp = 0;
            }
            
        }
        
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Lobster lobster = new Lobster();
        addObject(lobster,294,244);
        Bango bango = new Bango();
        addObject(bango,145,156);
        Crocodile crocodile = new Crocodile();
        addObject(crocodile,399,123);
    }
}
danpost danpost

2020/2/14

#
World world = getWorld();
world.removeObjects(world.getObjects(Lobster.class));
Lobster lobster = new Lobster();
world.addObject(lobster, 294, 244);
Rafiqi_Fariz Rafiqi_Fariz

2020/2/14

#
where i should place that code?
danpost danpost

2020/2/14

#
Rafiqi_Fariz wrote...
where i should place that code?
-- where "eater was died".
Rafiqi_Fariz Rafiqi_Fariz

2020/2/15

#
yeah it is working. Thank you very much danpost :)
You need to login to post a reply.