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

2013/3/11

Button not restarting game

xxhero24xx xxhero24xx

2013/3/11

#
I am trying to figure out a way to get my button to restart a game. The code is there but its like greenfoot doesn't know its there. Can you guys help me out?
public void gameOver()
    {
        isOver = true;
        removeObjects(getObjects(Actor.class));
        setBackground("Game Ending Image.png");
        addObject(stbutton, 417,370);
//        if (state==Gamestate.OVER)
        {
            
            checkbutton();
  //                  removeObjects(getObjects(Actor.class));
        }
    }
danpost danpost

2013/3/11

#
I seriously doubt that your 'checkButton()' call should be located in this method; but, probably, in your act method. Possibly with something like the following in the act method:
if (stbutton.getWorld() != null) checkbutton();
xxhero24xx xxhero24xx

2013/3/11

#
it is in my act method, but its like greenfoot doesn't read the code from start to end twice. Here's the entire code from my background actor:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class background  extends World
{
    button stbutton=new button();
    menubackground mbk=new menubackground();
    road rd=new road();
    road rd2=new road();
    enum Gamestate {MENU, PLAYING, OVER};
    Gamestate state;
    Score scr=new Score();
    Life life;
    LifeImg lifepic1, lifepic2, lifepic3;
    boolean isOver=false;
    int count;
    
    //Life life;
    /**
     * Constructor for objects of class background.
     * 
     */
    public background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addObject(mbk, 300,200);
        addObject(stbutton, 417,370);

        state=Gamestate.MENU;
    }

    public void act()
    {
        if (state==Gamestate.MENU)
            checkbutton();
        if (state==Gamestate.PLAYING)
            PlayGame();
        if(state == Gamestate.OVER && !isOver)
            gameOver();
    }

    public void checkbutton()
    {
        if (Greenfoot.mouseClicked(stbutton))
        {
            preparePlaying();
        }
    }

    public void preparePlaying()
    {
        removeObjects(getObjects(Actor.class));
        state=Gamestate.PLAYING;
        car racecar=new car();
        scr=new Score();
        life=new Life();
        addObject(scr, 80,50);
        addObject(life,300,50);
        addObject(rd,301,250);
        addObject(rd2,301,48);
        addObject(racecar, 294,364);
        lifepic1=new LifeImg();
        addObject(lifepic1,50,350);
        lifepic2=new LifeImg();
        addObject(lifepic2,100,350);
        lifepic3=new LifeImg();
        addObject(lifepic3,150,350);
    }

    public void PlayGame()
    {
        addObject(scr,75,45);
        addObject(life,300,75);
        int random = Greenfoot.getRandomNumber(100);
        if (random < 20)
        {
            int random2 = Greenfoot.getRandomNumber(50);
            if (random2==0&&count<=20)
            {
                traffic car = new traffic();
                addObject(car,Greenfoot.getRandomNumber(250)+50,0);
                count++;
            }
            if (random2==2&&count<=30)
            {
                traffic car = new traffic();
                addObject(car,Greenfoot.getRandomNumber(250)+250,0);
                count++;
            }
            if (random2==4&&count<=40)
            {
                traffic car = new traffic();
                addObject(car,Greenfoot.getRandomNumber(250)+500,0);
                count++;
            }
        }
        if (scr.getScore() >= 100)
        {
            state = Gamestate.OVER;
        }
        if (life.isDead())
        {
            state = Gamestate.OVER;
        }

    }


    public void addScore(int value)
    {
        scr.addScore(value);
    }

    public void removeLife(int value)
    {
        life.removeLife(value);
        removeLifeImage();
    }



    public void gameOver()
    {
        isOver = true;
        removeObjects(getObjects(Actor.class));
        setBackground("Game Ending Image.png");
        addObject(stbutton, 417,370);
//        if (state==Gamestate.OVER)
        {
            
            checkbutton();
  //                  removeObjects(getObjects(Actor.class));
        }
    }


    public void removeLifeImage()
    {
        if (life.getLifeCount()==1)
            removeObject(lifepic2);
        if(life.getLifeCount()==2)
            removeObject(lifepic3);
    }

    public void removeCarCount()
    {
        count--;
        if (count <0)count = 0;

    }

    }
let me know what you find.
danpost danpost

2013/3/11

#
You are not calling 'checkbutton()' for the specific state that your world is in. Try adding the following 'if' block to the 'act' method:
if (state == Gamestate.OVER && isOver)
{
    checkbutton();
}
xxhero24xx xxhero24xx

2013/3/11

#
that works. Thanks Dan
You need to login to post a reply.