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

2012/1/18

Multiple Conditions?

DMCGames DMCGames

2012/1/18

#
Im trying to make multiple conditions, when I put like 2 if(condition1 && Condition2) it works fine but when I put more than that, it doesn't work. Code:
        if (Greenfoot.mouseMoved(this))
        {  Box1 box1 = new Box1();
        Box2 box2 = new Box2();
        Box3 box3 = new Box3();
        Box4 box4 = new Box4();
        Box5 box5 = new Box5();
        Box6 box6 = new Box6();
        Box7 box7 = new Box7();
        Box8 box8 = new Box8();
        Box9 box9 = new Box9();
        getWorld().addObject(box1,200,184);
        getWorld().addObject(box2,218,184);
        getWorld().addObject(box3,236,184);
        getWorld().addObject(box4,200,202);
        getWorld().addObject(box5,218,202);
        getWorld().addObject(box6,236,202);
        getWorld().addObject(box7,200,220);
        getWorld().addObject(box8,218,220);
        getWorld().addObject(box9,236,220);
        if (box1.touches(iDiamond.class)&&box2.touches(iDiamond.class)&&box3.touches(iDiamond.class)&& box5.touches(Stick.class) && box8.touches(Stick.class))
        {
              getWorld().addObject(new DiamondPic (), 298,202);  
            getWorld().removeObjects(getWorld().getObjects(iDiamond.class)); 
            getWorld().removeObjects(getWorld().getObjects(Stick.class)); 
            
        }
        if(box5.touches(WoodenPlank.class) &&box8.touches(WoodenPlank.class))
        {
            getWorld().removeObjects(getWorld().getObjects(WoodenPlank.class)); 
            getWorld().addObject(new Stick(), 138,19);                                    
        }        
            
        if (Greenfoot.isKeyDown("x"))
        {
            getWorld().removeObject(this);
            
        }        
    }    
Builderboy2005 Builderboy2005

2012/1/18

#
By using multiple && statements, the IF will only execute if ALL of the conditions are true. Is this what you want? Because the way you formatted your If statement on line 20 is correct.
davmac davmac

2012/1/18

#
DMCGames: Define 'doesn't work'. Doesn't compile, or doesn't do what you expect?
DMCGames DMCGames

2012/1/19

#
I met all the conditions but it won't result in adding the object. I have a smaller version and it works fine. Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class InvCraft here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class InvCraft extends Actor
{
    /**
     * Act - do whatever the InvCraft wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseMoved(this))
        {
        Box1 box1 = new Box1();
        Box2 box2 = new Box2();
        Box3 box3 = new Box3();
        Box4 box4 = new Box4();
        getWorld().addObject(box1,90,12);
        getWorld().addObject(box2,101,11);
        getWorld().addObject(box3,83,29);
        getWorld().addObject(box4,101,29);
         if(box1.touches(WoodenPlank.class) &&box2.touches(WoodenPlank.class) &&box3.touches(WoodenPlank.class) &&box4.touches(WoodenPlank.class) )
        {
            getWorld().addObject(new WorkBench(), 138,19);
             getWorld().removeObjects(getWorld().getObjects(WoodenPlank.class));                          
        }          
         else if(box1.touches(WoodenPlank.class) &&box3.touches(WoodenPlank.class))
        {
            getWorld().addObject(new Stick(), 138,19);  
            getWorld().removeObjects(getWorld().getObjects(WoodenPlank.class));     
        }          
        else if (box2.touches(WoodenPlank.class) &&box4.touches(WoodenPlank.class))
        {
          getWorld().addObject(new Stick(), 138,19);
          getWorld().removeObjects(getWorld().getObjects(WoodenPlank.class));
        }       
      }
   }
}
Builderboy2005 Builderboy2005

2012/1/19

#
Try putting a System.out.println("Test") inside of the If statement. That way you will be able to tell what the problem is. Is the problem that the If statement isn't being triggered, or that the object isn't being added properly?
You need to login to post a reply.