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

2015/4/6

Stopping Greenfoot

RagingAsian RagingAsian

2015/4/6

#
How would I set up a counter object to stop the program when it reaches zero? I'm making a tower defense game and I want the game to stop when the lives counter has reached zero. Greep super class:

public class Greeps extends Actor
{   
    public int type;
    
    int[]WayX;
    int[]WayY;
    int index=0;
    
    double distance=150;
    
    public static int hp;
    public int speed;
    private int level;
    public Counter liveCounter;

    public Greeps(int hp,int speed, int lv,Counter lives)
    {
        level = lv;
        this.hp=hp;
        this.speed=speed;
        liveCounter=lives;
        setRotation(90); 
        if(type==1) setImage("Enemy-small.png");
        if(type==2) setImage("Enemy-medium.png");
        if(type==3) setImage("Enemy-heavy.png");
        waypoints();        
    }
    
    public void act() 
    {
        movement();
        gameOver();
    }                        
    
    /**
     * Sets waypoints where the Greeps will turn 
     *
     */
    public void waypoints()
    {

        if(level== 1)
        {
           WayX= new int[]{94,94,188,188,281,281,374,374,467,467,563,563,620};
           WayY= new int[]{108,223,223,28,28,372,372,83,83,314,314,140,140};
        
        }
        if (level == 2){
           WayX= new int[] {478,478,272,272,599};
           WayY= new int[]{171,380,380,19,19};
        }
        if (level == 3){
           WayX=new int[] {355,246,246,463,463,134,134,572,572,27,27};
           WayY=new int[] {248,248,115,115,314,314,49,49,381,381,10};
        }
        if (level == 4){
           WayX= new int[]{319,319,56,56,431,431,525,525,599};
           WayY= new int[]{376,21,21,288,288,21,21,375,375};
        }
    }
    
    /**
     * Sets the speed of the Greeps and determines
     * when to move onto the next waypoint
     */
    public void movement()
    {        
        move(1);
        int theDistance = (int)(Math.hypot(WayX[index] - getX(), WayY[index] - getY()));   
        if (theDistance < 1  )
        {
            if(index<WayX.length-1)
            {
                index++;
            } else
            {
                getWorld().removeObject(this);
                return;
            }
            
        } 
        turnTowards(WayX[index], WayY[index]);
       
    }   
    
    /**
      * Decreases the health of the Greep when it has been hit by a bullet
      *
      */
    public void decreaseHealth()
    {
        Actor b1 = getOneIntersectingObject(Bullets.class);
        Bullets b2 = (Bullets)b1;
        if(isTouching(Bullets.class))
        {
                                   
                   if(b2.bulletType==1)
                   {
                       hp=hp-20;
                       getWorld().removeObject(b1);
                       if (hp <= 0) getWorld().removeObject(this);
                   }
                   if(b2.bulletType==2)
                   {
                       hp=hp-10;
                       getWorld().removeObject(b1);
                       if (hp <= 0) getWorld().removeObject(this);
                   }
                   if(b2.bulletType==3)
                   {
                       hp=hp-10;
                       getWorld().removeObject(b1);
                       if (hp <= 0) getWorld().removeObject(this);
                   }
                   if(b2.bulletType==4)
                   {
                       hp=hp-45;
                       getWorld().removeObject(b1);
                       if (hp <= 0) getWorld().removeObject(this);
                   }
                   if(b2.bulletType==5)
                   {
                       hp=hp-35;
                       getWorld().removeObject(b1);
                       if (hp <= 0) getWorld().removeObject(this);
                   }                                                   
        }
        System.out.println(hp+"        hp");
        return;        
    }
    

    /**
      * Subtracts a life from the lives Counter
      * when a greep has reached the end of 
      * the path
      */
    public int loseALife()
    {       
        
        if((level==1)&&(index==12))
        {
            liveCounter.add(-1);
            getWorld().removeObject(this);  
              
        }
        if((level==2)&&(index>=4))
        {
            liveCounter.add(-1);   
            getWorld().removeObject(this);                                   
        }
        if((level==3)&&(index>=10))
        {
            liveCounter.add(-1);  
            getWorld().removeObject(this);                
        }
        if((level==4)&&(index>=8))
        {
            liveCounter.add(-1);     
            getWorld().removeObject(this);                                 
        }           
        return index;
    }    
    

   /**
     * Stops the game when there are no more 
     * lives left.
     */
    public void gameOver()
    {
        if (liveCounter)
        {
            Greenfoot.stop();
        }
    }
 }
danpost danpost

2015/4/6

#
Your Counter class probably has a 'getValue' method (or a method that returns its value) in it:
if (livesCounter.getValue() <= 0)
should suffice.
You need to login to post a reply.