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

2014/9/16

Help with boolean Statements

Loaf Loaf

2014/9/16

#
I have been trying to make a Hangman game however i have come across some trouble when it comes to making the game change the level to show the next word. I am currently trying to implement a Boolean statement that when all the letters have been pressed and set to true the game changes level to start the next word however when i check the game it says that all letters are true in the boolean statement yet nothing happens. This is my code can anyone help me out.
public static boolean W = false;
public static boolean R = false;
public  boolean E = false;
public static  boolean N = false;
public static boolean C = false;
public static boolean H = false;
    /**
     * Constructor for objects of class Prision.
     * 
     */
    public Prison()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
     
        GreenfootImage background = getBackground();
            background.drawString("Plumbers use this to fix toilets.", 20, 580);// this is the description for the word
         addObject(new W(), 200, 150);// this spawns in all of the letters
        addObject(new R(), 200, 190);
         addObject(new E(), 200, 230);
          addObject(new N(), 200, 270);
           addObject(new C(), 200, 310);
            addObject(new H(), 200, 350);
            addObject(new A(), 200, 390);
              addObject(new B(), 200, 430);
                addObject(new D(), 200, 470);
                  addObject(new F(), 260, 160);
                    addObject(new G(), 260, 200);
                      addObject(new I(), 260, 240);
                        addObject(new J(), 260, 280);
                          addObject(new K(), 260, 320);
                            addObject(new L(), 260, 360);
                              addObject(new M(), 260, 400);
                                addObject(new N(), 260, 440);
                                  addObject(new O(), 310, 150);
                                    addObject(new P(), 310, 190);
                                      addObject(new Q(), 310, 230);
                                        addObject(new S(), 310, 270);
                                          addObject(new T(), 310, 310);
                                            addObject(new U(), 310, 350);
                                              addObject(new V(), 310, 390);
                                                addObject(new X(), 310, 430);
                                                  addObject(new Y(), 310, 110);
                                                    addObject(new Z(), 200, 110);
            
            addObject(new Timer(), 60, 50);
        
       
          
       
        addObject(new line(), 180, 550);
        addObject(new line(), 220, 550);
        addObject(new line(), 260, 550);
        addObject(new line(), 300, 550);
        addObject(new line(), 340, 550);
        addObject(new line(), 380, 550);

       

        background.drawLine(500, 60, 350, 60); // this creates the hangman noose
        background.drawLine(350, 60, 350, 480);
        background.drawLine(350, 480, 500, 480);
        
        background.drawLine(0, 440, 200, 440);
        background.drawLine(40, 440, 40, 120);
        background.drawLine(40, 120, 160, 120);
        background.drawLine(160, 120, 160, 140);
    
    }        public  void setW() 
        { 
            W = true;
        }
       

         public  void setR() 
        { 
            R = true;
        }
       
         public  void setE() 
        { 
            E = true;
        }
         
         public  void setN()  
        { 
            N = true;
        }
        
         public  void setC() 
        { 
            C = true;
        }
         
         public  void setH()  
        { 
            H = true;
        }
          
        public void change()
        {
            if ( W== true && R == true && E == true && N == true && C == true && H == true )
            {
            Greenfoot.setWorld(new Prison2());
            W = false;
             R = false;
             E = false;
             N = false;
             C = false;
             H = false;
            }
        }
}
Super_Hippo Super_Hippo

2014/9/16

#
Do you call the method 'change' somewhere? Either you have to call it after all 'set' methods, or you name it 'act', then it will be executed every time. Since your booleans are static (why?), you should set them to 'false' in the constructor and not outside methods. Else, they will be true, when you reset the game after they were true. By the way:
//instead of this:
if ( W== true && R == true && E == true && N == true && C == true && H == true )
//you can also just write this:
if (W && R && E && N && C && H)
Do you want to create a new world for every word? This will be much work I guess.
danpost danpost

2014/9/16

#
You need to show the related code where you are calling these 'set' methods.
You need to login to post a reply.