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

2014/1/8

Greenfoot stop

1
2
Aaron91 Aaron91

2014/1/8

#
Got a question about my boot actor. It has 3 hearts, but greenfoot continues after all hearts are depleted. What am i doing wrong this time.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class boot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class boot extends Actor
{
    /**
     * Act - do whatever the boot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        
        
        if (Greenfoot.isKeyDown("left"))
        {
         move(-3);   
        }
        if (Greenfoot.isKeyDown("right"))
        {
         move(3);   
        }
         if (Greenfoot.isKeyDown("up"))
        {
         setRotation(90);
         move(-3);
         setRotation(0);
        }
        if (Greenfoot.isKeyDown("down"))
        {
         setRotation(-90);
         move(-3);
         setRotation(0);   
        }     
        
        
    }    
    
    private void hit()
    {
        Actor barrel;
        barrel = getOneObjectAtOffset(0, 0,barrel.class);
        if (barrel != null)
        {
          World world;
          world = getWorld();
          world.removeObject(barrel);
          
        }       
    }
    
    public boot()  
    {      
        GreenfootImage image = getImage();    
        image.scale(230, 130);  
        setImage(image);  
    }
    
    public void removeLive()    
    {    
        int lives = getWorld().getObjects(heart.class).size();    
        getWorld().removeObject((Actor)getWorld().getObjects(heart.class).get(lives-1));  

        if (lives != 1)  
        {  
            //
        }  
        else  
        {     
            Greenfoot.stop();  
        }  
    }   
    
    

} 
danpost danpost

2014/1/8

#
Nowhere do I see the 'removeLive' method being called.
Aaron91 Aaron91

2014/1/8

#
But when i try to call it in the act method. Greenfoot stops immediatly.
danpost danpost

2014/1/8

#
I think the project you got the lives code from had multiple heart objects and you only have one. I cannot be sure because you have given no background info on the heart object(s) you are using. Maybe not. Just a second.
Aaron91 Aaron91

2014/1/8

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

{

  public static final boolean VERTICAL = true;  
  public static final boolean HORIZONTAL = false;  
  private static final int GAP = 10;  
  private boolean orientation = HORIZONTAL;  
  private GreenfootImage icon;  
  private int count;
  private boolean direction;
  private int initValue;  
  private int value;
  

  
  public heart()  
  {  
    icon = new GreenfootImage("herz.png");  
    count = 3;
    direction = false;
    value = initValue = count;  
    orientation = direction;  
    updateImage();
    
    
  } 
  
  private void updateImage()  
  {  
    int across = value;  
    int down = 1;  
    int xGap = GAP;  
    int yGap = 0;  
    if (orientation == VERTICAL)  
    {  
      across = 1;  
      down = value;  
      xGap = 0;  
      yGap = GAP;  
    }  
    int wide = icon.getWidth()*across+xGap*(across+1);  
    int high = icon.getHeight()*down+yGap*(down+1);  
    GreenfootImage base = new GreenfootImage(wide, high);  
    for (int j=0; j<down; j++) for (int i=0; i<across; i++)  
    {  
      int xOffset =xGap+i*(xGap+icon.getWidth());  
      int yOffset = yGap+j*(yGap+icon.getHeight());  
      base.drawImage(icon, xOffset, yOffset);  
    }  
    setImage(base);  
  }  
  
  public void adjustValue(int change)  
  {  
    if (value+change > initValue || value + change < 0) return;  
    value += change;  
    updateImage();  
  }  
  
  public int getValue()  
  {  
    return value;  
  }  
  
  
    }
This is the heart class i use. the only other thing i did with the lives is calling them in the world class.
danpost danpost

2014/1/8

#
I think you put just 'removeLive();' in the act method. You need a condition on that.
if (/* some condition */) removeLive();
Possibly using a detection method on another object type.
Aaron91 Aaron91

2014/1/8

#
in the condition.. should i refer back to the Heart.class? saying if that equals 0? or how does that work? cause i usually call it just like 'removeLive();'
danpost danpost

2014/1/8

#
Answer the question "when do I want a heart to be removed?'. The answer to that will determine the condition. Otherwise, if you start with three hearts, and you remove one each act cycle, the game will end in just three cycles (approximately 1/20 of a second).
Aaron91 Aaron91

2014/1/8

#
My game starts with 3 hearts and are removed each time my boot actor hits a barrel actor. So trying to get the game stop after the hearts run out.
danpost danpost

2014/1/8

#
NVM, mis-read your response. One moment please.
danpost danpost

2014/1/8

#
The code executed when that condition occurs is in the 'hit' method, in the 'if (barrel != null)' block. That is where you place your call to 'removeLive' (around line 53 of the boot class).
Aaron91 Aaron91

2014/1/8

#
nvm this response .. just saw your last message..
Aaron91 Aaron91

2014/1/9

#
and i am guessing that the 'removeLive();" needs an nested if inside the 'hit' method?
danpost danpost

2014/1/9

#
The 'if' block was already there; just add 'removeLive();' inside it.
Aaron91 Aaron91

2014/1/9

#
private void hit()
    {
        Actor barrel;
        barrel = getOneObjectAtOffset(0, 0,barrel.class);
        if (barrel != null)
        {
          World world;
          world = getWorld();
          world.removeObject(barrel);
          removeLive();
          
     
        }       
    }
    
public void removeLive()    
    {    
        int lives = getWorld().getObjects(heart.class).size();    
        getWorld().removeObject((Actor)getWorld().getObjects(heart.class).get(lives-1));  

        if (lives != 1)  
        {  
            //
        }  
        else  
        {     
            Greenfoot.stop();  
        }  
    }   
did this, but the game still continues after the hearts are depleted.
There are more replies on the next page.
1
2