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

2022/11/26

How do I set the enemyHealth counter to not go below zero?

Prannn Prannn

2022/11/26

#
import greenfoot.*;  //here's my code for the enemy

/**
 * Write a description of class EnemyPlane here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class EnemyPlane extends Actor
{
    /**
     * Act - do whatever the EnemyPlane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int speed = 3;
    int count = 0;
    int delay = 0;
    int health = 5;

    public void move()
    {
        Stage2.heli.playLoop();
        if(count < 240)
        setLocation(getX() - speed,getY());
        else 
        {
            speed = - speed;
            getImage().mirrorHorizontally();
            count = 0;
        }
    }
    public void hit()
    {
        if(isTouching(RightBullet.class))
        {
            removeTouching(RightBullet.class);
            getWorld().addObject(new blast(),getX(),getY());
            Stage2.score.add(5);
            Stage2.EnemyHealth.add(-1);
        }
    }
    public void win()
    {
        if(Stage2.EnemyHealth.getValue()<=0 && Stage2.EnemyHealth1.getValue()<=0)
        {
            getWorld().addObject(new Win(),getWorld().getWidth()/2,getWorld().getHeight()/2);
            getWorld().addObject(new next1(), 950,310);
            Stage2.mainBG.stop();
            MyWorld.win.playLoop();
            Stage2.heli.pause();
        }
    }
    public void shoot()
    {
        delay++;
        if(delay==120)
        {
            getWorld().addObject(new PlaneBullet(), getX(),getY());
            delay=0;
            Greenfoot.playSound("missile.mp3");
        }
    }
    public void act()
    {
        shoot();
        count++;
        hit();
        move();
        win();
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    /**
     * Constructor for objects of class Stage2.
     * 
     */
    public static Counter health = new Counter("Health : ");
    public static Counter score = new Counter("Score : ");
    public static Counter EnemyHealth = new Counter("Enemy Health : ");
    public static Counter EnemyHealth1 = new Counter("Enemy 2 Health : ");
    public static GreenfootSound mainBG = new GreenfootSound ("bgsound.mp3");
    public static GreenfootSound heli = new GreenfootSound ("heli.mp3");
    public static GreenfootSound closing = new GreenfootSound ("Closing.mp3");
    public Stage2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 350, 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()
    {
        player1 player1 = new player1();
        addObject(player1,60,260);
        EnemyPlane enemyPlane = new EnemyPlane();
        addObject(enemyPlane,900,40);
        EnemyPlane1 enemyPlane1 = new EnemyPlane1();
        addObject(enemyPlane1,65,30);
        addObject(score,50,20);
        score.setValue(0);
        addObject(health,500,20);
        health.setValue(5);        
        addObject(EnemyHealth,900,20);
        EnemyHealth.setValue(10);
        addObject(EnemyHealth1,690,20);
        EnemyHealth1.setValue(10);
    }
}
danpost danpost

2022/11/27

#
Prannn wrote...
How do I set the enemyHealth counter to not go below zero?
Putting the following line after line 39 in EnemyPlane class might help:
if (Stage2.EnemyHealth.getValue() < 0) Stage2.EnemyHealth.add(1);
You need to login to post a reply.