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

2016/10/2

gameover

Dayuyu Dayuyu

2016/10/2

#
i when to make gameover when main actor do not have live but i have some problem at zero life.. (life<=0) it say bad operand type for binary operator... gameover
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class gameover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gameover extends Actor
{
    /**
     * Act - do whatever the gameover wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    Button tryAgainButton;
    public boolean buttonsPlaced;
    public Gameover()
    {
        setImage(new GreenfootImage("GAME OVER", 48, Color.WHITE, Color.BLACK));
        tryAgainButton=new Button();
        buttonsPlaced=false;
    }

    public void act()
    {
        if(!buttonsPlaced)
        {
            place();
            buttonsPlaced=true;
        }
        detectClick();
    }
    
    public void place()
    {
        getWorld().addObject(tryAgainButton, getWorld().getWidth()/2, 250);
        tryAgainButton.setImage("Kuy.png");
    }
    
    public void detectClick()
    {
        if(Greenfoot.mouseClicked(tryAgainButton))
        {
            Greenfoot.setWorld(new Background1());
        }
    }
}
main actor
    Life life;
    public void kurang()
    {
        Actor Ular;
        Ular = getOneObjectAtOffset(4, 4, Ular.class);
        if(Ular != null)
        {
            Background1.getLife().life--;
            if(life<=0)
            {
              Gameover gameover = new Gameover();
            }
        }
    }
danpost danpost

2016/10/2

#
In the main actor class code above: (1) remove line 1; and (2) change line 9 to this:
if (Background1.getLife().life <= 0)
Dayuyu Dayuyu

2016/10/2

#
nothing happen...life still can reach negative value...
danpost danpost

2016/10/2

#
Dayuyu wrote...
nothing happen...life still can reach negative value...
It does no good to create a Gameover world and not set it active.
Dayuyu Dayuyu

2016/10/2

#
how i supposed to set it active.. i create new world game... i do not know if it right... game world
    public Game()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addObject(new Gameover(),300,140);
    }
main actor
    public void kurang()
    {
        Actor Ular;
        Ular = getOneObjectAtOffset(4, 4, Ular.class);
        if(Ular != null)
        {
            Background1.getLife().life--;
            if(Background1.getLife().life <= 0)
            {
              Game game = new Game();
            }
        }
    }
gameover
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class gameover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gameover extends Actor
{
    /**
     * Act - do whatever the gameover wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    Button tryAgainButton;
    public boolean buttonsPlaced;
    public Gameover()
    {
        setImage(new GreenfootImage("GAME OVER", 48, Color.WHITE, Color.BLACK));
        tryAgainButton=new Button();
        buttonsPlaced=false;
    }

    public void act()
    {
        if(!buttonsPlaced)
        {
            place();
            buttonsPlaced=true;
        }
        detectClick();
    }
    
    public void place()
    {
        getWorld().addObject(tryAgainButton, getWorld().getWidth()/2, 250);
        tryAgainButton.setImage("Kuy.png");
    }
    
    public void detectClick()
    {
        if(Greenfoot.mouseClicked(tryAgainButton))
        {
            Greenfoot.setWorld(new Background1());
        }
    }
}
Dayuyu Dayuyu

2016/10/2

#
i done it thank for help
You need to login to post a reply.