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

2013/10/31

Help

tartags11 tartags11

2013/10/31

#
So I am trying to get bet() to run until a bet is selected, and then no longer run. However, it won't work. Even if the value changes to 1, 2, 3, or 4, bet doesn't run. If i try to run bet() outside of an if statement, it works but adds infinite amount of horses until Greenfoot.stop;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class RaceWorld extends World
{  
        
    /**
     * Constructor for objects of class RaceWorld.
     * 
     */
    public RaceWorld()
    {    
        // Create a new world with 1200x600 cells with a cell size of 1x1 pixels.
        super(1200, 600, 1);
        addObject( new BetWindow(), 570, 440);
        addObject( new HorseBet(), 1125, 490);
        addObject( new Money(), 1110, 35);
    }
    
    public void act()
    {
        int value = ((HorseBet) getObjects(HorseBet.class).get(0)).getValue();
        bet();
        if ( value == 0)
        {
            bet();
        }
        else
        {
            //nothing
        }
    }
    
    public void bet()
    {
        int value = ((HorseBet) getObjects(HorseBet.class).get(0)).getValue();
        if ( value == 1)
        {
            removeObjects(getObjects(BetWindow.class));
            addObject( new Horse1(), 50, 80);
            addObject( new Horse2(), 50, 152);
            addObject( new Horse3(), 50, 222);
            addObject( new Horse4(), 50, 296);
        }
        else if ( value == 2)
        {
            removeObjects(getObjects(BetWindow.class));
            addObject( new Horse1(), 50, 80);
            addObject( new Horse2(), 50, 152);
            addObject( new Horse3(), 50, 222);
            addObject( new Horse4(), 50, 296);
        }
        else if ( value == 3)
        {
            removeObjects(getObjects(BetWindow.class));
            addObject( new Horse1(), 50, 80);
            addObject( new Horse2(), 50, 152);
            addObject( new Horse3(), 50, 222);
            addObject( new Horse4(), 50, 296);
        }
        else if ( value == 4)
        {
            removeObjects(getObjects(BetWindow.class));
            addObject( new Horse1(), 50, 80);
            addObject( new Horse2(), 50, 152);
            addObject( new Horse3(), 50, 222);
            addObject( new Horse4(), 50, 296);
        }
        else
        {
            //nothing
        }
    }
    
    public void started()
    {
        //code
    }
}
Yogibaer Yogibaer

2013/11/4

#
hallo tartags11, you declare the variable "value" within the act-method and within the bet()-method. In this moment eachtime the value of "value" is set to sero. I believe this is not what you want. Declare singing variables outside and try again. greetings from Yogibaer
You need to login to post a reply.