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

2013/2/11

If statements aren't working in Greenfoot

student101 student101

2013/2/11

#
I've been trying to get an actor to perform the conditional that's within it's act section, however Greenfoot doesn't seem to want to run the code. I've triple checked it to make sure the syntax is at least correct and compiling it doesn't give any errors. It's just that when I run my program, Greenfoot just seems to ignore it. To test this I've written a code to print out 'test' if it reaches the area of the code where the conditional is located, and once again, Greenfoot ignores it. Putting the test code before the conditional works however.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class StatusBar1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StatusBar1 extends Actor
{
    /**
     * Act - do whatever the StatusBar1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    World world = getWorld();
    public void act() 
    {
        if (world instanceof BattleWorld || world instanceof AnswerCorrectWorld || world instanceof InvItemsNone)
        {
         getImage().setTransparency(0);
        }
         if (world instanceof BattleFight)
        {
            getImage().setTransparency(255);
            setImage("status-bar-inverted.png");
            
        }
        if (world instanceof BattleWorld2)
        {
            getImage().setTransparency(0);
        }
        
     /*   if (world instanceof AnswerCorrectWorld)
        {
            getImage().setTransparency(0);
        }*/
        if (world instanceof GameWorldAfterBattle || world instanceof BattleFight2)
        {
            getImage().setTransparency(255);
            
        }
        /*if (world instanceof InvItemsNone)
        {
             getImage().setTransparency(0);
        }  
    
   
      
        if (world instanceof BattleWorld2)
        {
            getImage().setTransparency(0);
        }*/
     /*   if (world instanceof BattleFight2)
        {
            getImage().setTransparency(255);
        }*/
       
    }
}
davmac davmac

2013/2/11

#
line 15 should be inside the act method. Where it is now, it will be executed when the actor is constructed - at which point it's not in any world, so getWorld() returns null. As null is not an instance of any class, all the conditions in the "if" statements are false.
student101 student101

2013/2/11

#
Thank you davmac. I can't believe the whole time it was just that one line. I've been stressing myself over it for the past few days. But once again thank you.
student101 student101

2013/2/11

#
It seems like that code wasn't the only problem. If statements here and there aren't working too. I'm trying to make an actor change its image when more than one conditions are met, but it will refuse to work properly:
  boolean Status80 = false;
    boolean Status60 = false;
    
    public void act() 
    {
        CheckImage();
        CheckWorld();
        
    }
    public void CheckImage()
    {
        World world = getWorld();
       
        if (world instanceof BattleFightAttack)
        { 
            
            Actor arrow = (Actor)getWorld().getObjects(BattleArrow.class).get(0); 
          if (arrow.getX()==325 && arrow.getY()==335 && ("enter".equals(Greenfoot.getKey())))
        {
           
         //  if (arrow.getX()==325 && arrow.getY()==335 && ("enter".equals(Greenfoot.getKey())))
           
          //  arrow.setLocation(41, 332);
       
            setImage("status-bar-80-inverted.png");
            Status80 = true;   
             
          
        }
    }
    }
davmac davmac

2013/2/11

#
It's not that the if statements aren't working; it's just that the conditions you set aren't being met. It's hard to tell why that's the case without seeing all of your code. If you put in code to print out the value of arrow.getX() and arrow.getY() you may find that the object is never at the exact coordinates you've specified (i.e 325,335) for some reason. Or perhaps you're calling Greenfoot.getKey() from some other actor which is "eating" the keypress.
student101 student101

2013/2/11

#
Davmac once again you was right. Another actor was eating the keypress. I've got yet another problem though. The StatusBar1 actor is meant to check whether a boolean is true or false to determine it's image once it's in a specific world. Once again, the if statements doesn't seem to be working. I've checked if there were any conflicting actors in the world and I think there aren't any. I'm talking about the GameAfterBattle world in the CheckWorld method.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class StatusBar1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StatusBar1 extends Actor
{
    /**
     * Act - do whatever the StatusBar1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    boolean Status80 = false;
    boolean Status60 = false;
     int counter;
    
    public void act() 
    {   World world = getWorld();
        if (world instanceof BattleFightAttack)
        {if (counter>0)  
        {  
            Actor falsepopup = (Actor)getWorld().getObjects(FalsePopup.class).get(0);
            counter--;  
            if(counter==0) falsepopup.getImage().setTransparency(0); 
            return;
        }}
        
        CheckImage();
        CheckWorld();
        
    }
   
    public void CheckImage()
    {
        World world = getWorld();
       
        if (world instanceof BattleFightAttack)
        {    
          Actor arrow = (Actor)getWorld().getObjects(BattleArrow.class).get(0); 
          Actor falsepopup = (Actor)getWorld().getObjects(FalsePopup.class).get(0);
            if (arrow.getX()==325 && arrow.getY()==335 && ("enter".equals(Greenfoot.getKey())))
            {
                falsepopup.getImage().setTransparency(255);
                counter = 180;
                setImage("status-bar-80-inverted.png");
                Status80 = true;                    
            }
            else 
            {
                Status80 = false;
            }
        }
    }
    public void CheckWorld()
    {
          World world = getWorld();
        if (world instanceof BattleWorld || world instanceof AnswerCorrectWorld || world instanceof InvItemsNone)
        {
         getImage().setTransparency(0);
        }
         if (world instanceof BattleFight)
        {
            getImage().setTransparency(255);
            setImage("status-bar-inverted.png");
            
        }
        if (world instanceof BattleWorld2)
        {
            getImage().setTransparency(0);
        }
        if (world instanceof GameWorldAfterBattle)  
        {
            getImage().setTransparency(255);
            if (Status80 = false)
            {
                setImage("status-bar.png");
                
            }
            else
            {   
                setImage("status-bar-80.png");
             //   System.out.println("Test");
            }
        }
        if (world instanceof BattleFight2)
        {
            getImage().setTransparency(255);
            if (Status80 = true)
            {
                setImage("status-bar-80.png");
            }
            else
            {
                setImage("status-bar.png");
            }
        }
    }
}
davmac davmac

2013/2/12

#
Line 90 is the problem:
            if (Status80 = true)  
A single '=' is assignment, not comparison. It should be:
            if (Status80 == true)  
Or even just:
            if (Status80)  
(You don't need to compare a boolean to true; it is already either true or false).
student101 student101

2013/2/12

#
Thank you davmac! I've realised that the problems don't lie in Greenfoot, they lie in my lack of coding ability. Once again I've been trying to get yet another conditional statement to work. There is an actor called StatusBar that runs several checks in order to display its right image. It checks which world it's in as well as the boolean Status80 or Status60 is true or false. I've gotten the code to work for Status80, yet trying to check for Status60 is proving to not work?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class StatusBar1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StatusBar1 extends Actor
{
    /**
     * Act - do whatever the StatusBar1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    boolean Status80 = false;
    boolean Status60 = false;
     int counter;
    
    public void act() 
    {   World world = getWorld();
        CheckImage();
        CheckWorld();
        
        if (world instanceof BattleFightAttack || world instanceof BattleFightAttack2)
        {   if (counter>0)  
           {  
            Actor falsepopup = (Actor)getWorld().getObjects(FalsePopup.class).get(0);
            counter--;  
            if(counter==0) falsepopup.getImage().setTransparency(0); 
            return;
            }
        }
        CheckStatus();
    }
   
    public void CheckImage()
    {
        World world = getWorld();
       
        if (world instanceof BattleFightAttack)
        {    
          Actor arrow = (Actor)getWorld().getObjects(BattleArrow.class).get(0); 
          Actor falsepopup = (Actor)getWorld().getObjects(FalsePopup.class).get(0);
            if (arrow.getX()==325 && arrow.getY()==335 && ("enter".equals(Greenfoot.getKey())))
            {
                falsepopup.getImage().setTransparency(255);
                counter = 180;
                setImage("status-bar-80-inverted.png");
                Status80 = true;                    
            }
            
        }
        if (world instanceof BattleFightAttack2)
        {
            
          Actor arrow = (Actor)getWorld().getObjects(BattleArrow.class).get(0); 
          Actor falsepopup = (Actor)getWorld().getObjects(FalsePopup.class).get(0);
           if (arrow.getX()==325 && arrow.getY()==335 && ("enter".equals(Greenfoot.getKey())))
            {
             //   System.out.println("Test");
                falsepopup.getImage().setTransparency(255);
                counter = 180;
            }
            if (Status80 == true)
                {
                    Status60 = true;
                    
                 //   CheckWorld();
                }
                              
            }
        }
    
    public void CheckStatus()
    {   World world = getWorld();
        if (world instanceof BattleFight || world instanceof BattleFight2)
        {
            if (Status80 == false || Status60 == false)
            {
                setImage("status-bar-inverted.png");
            }
            if (Status80 == true)
            {
                setImage("status-bar-80-inverted.png");
            }
            if (Status60 == true)
            {
                setImage("status-bar-60-inverted.png");   
            }
    
    }
}
    public void CheckWorld()
    {
          World world = getWorld();
        if (world instanceof BattleWorld || world instanceof AnswerCorrectWorld || world instanceof InvItemsNone)
        {
         getImage().setTransparency(0);
        }
      /*   if (world instanceof BattleFight)
        {
            getImage().setTransparency(255);
            setImage("status-bar-inverted.png");
            
        }*/
        if (world instanceof BattleWorld2)
        {
            getImage().setTransparency(0);
        }
        if (world instanceof GameWorldAfterBattle)  
        {
            getImage().setTransparency(255);
            if (Status80 == true)
            {
                setImage("status-bar-80.png");
            }
            else
            {   
                setImage("status-bar.png");
              
            }
        }
        if (world instanceof BattleFight || world instanceof BattleFight2)
        {
            getImage().setTransparency(255);
         /*   if (Status80 == false || Status60 == false)
            {
                setImage("status-bar-inverted.png");
            }
            if (Status80 == true)
            {
                setImage("status-bar-80-inverted.png");
            }
            if (Status60 == true)
            {
                setImage("status-bar-60-inverted.png");   
            }*/
        }
    }
}
davmac davmac

2013/2/13

#
Can you be more precise about which part isn't working and in what way? Is it lines 86 - 89? Are you sure that Status60 has actually been set to true?
student101 student101

2013/2/13

#
Yeah, I meant lines 86-89. It is set to true in line 66, however I don't think the code actually sets it to true, probably due to an error on my part. I just essentially want the actor to check if Status60 is true or not and set its image to the right one.
student101 student101

2013/2/13

#
Nevermind, I managed to solve the problem on my own!
You need to login to post a reply.