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

2017/3/13

Variables not taking on the proper boolean (true or false)

abby410 abby410

2017/3/13

#
I am trying to create a game that when two pictures are connected and correctly corresponding a new screen or object appears. It seems as though my objects are not taking on the variables that they should be. This is what happens when the 2 objects collide. I know that part of it is working because the new Greenfoot Image "IntAnswer3" is drawn. The problem is that the object Int3 is not taking on the connected or correct variables that I need it to. How do I fix this?
 private void collision1(){
        LogarithmInteractive interactive = (LogarithmInteractive)getWorld();
        if (removed == false){
            if (!interactive.getObjects(IntPlaceholder2.class).isEmpty()) {
                Actor int2Img = getOneIntersectingObject(IntPlaceholder2.class);
                if (int2Img != null){
                    getWorld().removeObject(int2Img);
                    GreenfootImage image = new GreenfootImage("IntAnswer3.png");
                    getWorld().getBackground().drawImage(image,490,35);
                    getWorld().removeObject(this);
                    connected = true; 
                    correct = false;
                    removed = true;
                }     
            }   
        }        
    }  
Super_Hippo Super_Hippo

2017/3/13

#
These variables are set to the correct values, but well, you remove the object, so it is gone.
abby410 abby410

2017/3/13

#
I am no longer removing the object, but the world still is not getting the correct boolean values. Any other suggestions?
Super_Hippo Super_Hippo

2017/3/13

#
Where do you try to give the values to the world?
abby410 abby410

2017/3/13

#
Here is the world class. The boolean variable values should transfer to this and then enact this code.
 /**
     * This method will run the complete() method to check
     * if all answers are complete.
     */
    public void act(){
        if(int2.connected == true && int3.connected == true && int9.connected == true)
        {
          
           complete();
        }
    }
    
    /**
     * This method checks to see if all drag and drop answers have been snapped to the correct positions.
     * Once all have been done, a message is displayed.
     */
    public void complete(){
        if ((int2.connected == true && int2.correct == true) && (int3.connected == true && int3.correct == true) && (int9.connected == true && int9.correct == true))
        {
            //JOptionPane.showMessageDialog(null, "Well done. You have correctly completed the logarithm.");int2.connected = false;
            Help help = new Help();
            addObject(help, 512, 384);
            
            int2.connected = false;
            int3.connected = false;
            int9.connected = false;
        } else if (int2.correct == false || int3.correct == false || int9.correct == false) 
        {
            //JOptionPane.showMessageDialog(null, "Not all the answers are in the correct positions. Try again.");
            
            GreenfootImage image = new GreenfootImage("cactus.png");
            getBackground().drawImage(image,150, 360);
          
            if (reset == true){
                Greenfoot.setWorld(new LogarithmInteractive());   
            }
        }
    }
 
    }
danpost danpost

2017/3/13

#
Please show the entire Int3 class code. I have a feeling that the problem may be elsewhere in the class.
abby410 abby410

2017/3/13

#
Here is the entire Int3 class code.
public class Int3 extends Actor
{//this boolean variable will check to see if the Message object has been grabbed
    private boolean isGrabbed;
    //this variable will check when this object has collided with the corresponding placeholder
    public boolean connected = false;    
    
    public boolean correct = false;     
    private boolean removed = false;
    /**
     * This method checks for whether or not it is being dragged by checking in the 
     * mouse has been pressed on this Actor and the isGrabbed boolean is checked to true or
     * false accordingly. 
     * The object position is tracked according to the mouseinfo X and Y details.
     * The object is added to the world according to this position.
     */
    public void act() 
    {
        // check if the mouse button is pressed on this object, and isGrabbed is false
        if (Greenfoot.mousePressed(this) && !isGrabbed){
            //set isGrabbed to true, meaning the object is now grabbed
            isGrabbed = true;
            // keep this dragged object on top of other objects
            World world = getWorld();
            MouseInfo mInfo = Greenfoot.getMouseInfo();
            world.removeObject(this);
            world.addObject(this, mInfo.getX(), mInfo.getY());
            return;
        }
        
        // if isGrabbed = true follow the mouse position
        if ((Greenfoot.mouseDragged(this)) && isGrabbed){
            MouseInfo mInfo = Greenfoot.getMouseInfo();
            setLocation(mInfo.getX(), mInfo.getY());
            return;
        }
        
        // if object 'isGrabbed' and is not being dragged then set isGrabbed to false and check
        // if the object has collided with the correct answer placeholder
        if (Greenfoot.mouseDragEnded(this) && isGrabbed){
            collision1();
            collision2();
            collision3();
            
            // release the object
            isGrabbed = false;
            return;
        }
    }
    
    /**
     * This method checks to see if the object has collided with the placeholder.
     * If it has, then remove this object and the placeholder and add an image to show the correct answer.
     */
    private void collision1(){
        LogarithmInteractive interactive = (LogarithmInteractive)getWorld();
        if (removed == false){
            if (!interactive.getObjects(IntPlaceholder2.class).isEmpty()) {
                Actor int2Img = getOneIntersectingObject(IntPlaceholder2.class);
                if (int2Img != null){
                    getWorld().removeObject(int2Img);
                    GreenfootImage image = new GreenfootImage("IntAnswer3.png");
                    getWorld().getBackground().drawImage(image,490,35);
                    //getWorld().removeObject(this);
                    connected = true; 
                    correct = false;
                    removed = true;
                }     
            }   
        }        
    }  
    
    /**
     * This method checks to see if the object has collided with the placeholder.
     * If it has, then remove this object and the placeholder and add an image to show the correct answer.
     */
    private void collision2(){        
        LogarithmInteractive interactive = (LogarithmInteractive)getWorld();
        if (removed == false){
            if (!interactive.getObjects(IntPlaceholder3.class).isEmpty()) {        
                Actor int3Img = getOneIntersectingObject(IntPlaceholder3.class);
                if (int3Img != null){
                    getWorld().removeObject(int3Img);
                    GreenfootImage image = new GreenfootImage("IntAnswer3.png");
                    getWorld().getBackground().drawImage(image,200, 85);
                    //getWorld().removeObject(this);
                    connected = true;            
                    correct = true;
                    removed = true;
                }
            }
        }
    }               
    
    /**
     * This method checks to see if the object has collided with the placeholder.
     * If it has, then remove this object and the placeholder and add an image to show the correct answer.
     */
    private void collision3(){
        LogarithmInteractive interactive = (LogarithmInteractive)getWorld();
        if (removed == false){
            if (!interactive.getObjects(IntPlaceholder9.class).isEmpty()) {
                Actor int9Img = getOneIntersectingObject(IntPlaceholder9.class);
                if (int9Img != null){
                    getWorld().removeObject(int9Img);
                    GreenfootImage image = new GreenfootImage("IntAnswer3.png");
                    getWorld().getBackground().drawImage(image,310,35);
                    //getWorld().removeObject(this);
                    connected = true;            
                    correct = false;
                    removed = true;
                }
            }
        }        
    }  
    
}

   
danpost danpost

2017/3/13

#
I do not see anything amiss in that class. The most likely place now would be in your LogarithmInteractive class. Let us see that one. The most probable cause in that class would be that you used 'new Int3()' more than once in the class. If that is the case, then you probably did the same with the other int#s as well.
abby410 abby410

2017/3/14

#
Here is my LogarithmInteractive class:
/**
 * Write a description of class LogarithmInteractive here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LogarithmInteractive extends World
{
 //Objects are created to be added to the world as needed
   IntPlaceholder2 intplaceholder2 = new IntPlaceholder2();
   IntPlaceholder3 intplaceholder3 = new IntPlaceholder3();
   IntPlaceholder9 intplaceholder9 = new IntPlaceholder9();
 
    Int2 int2 = new Int2();
    Int3 int3 = new Int3();
    Int9 int9 = new Int9();    
    
    
    public boolean reset = false;
    /**
     * Constructor for objects of class LogarithmInteractive.
     * Creates a World with a screen size of 600*400 and cell
     * sizes of 1 by 1 pixels. 
     */
    public LogarithmInteractive()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
    
    public void prepare()
    {
        //navigation buttons added to the bottom left of the screen
        //BackToMain backtomenu = new BackToMain();
        //addObject(backtomenu,55,720);                     
        //ResetLog resetE = new ResetLog();
        //addObject(resetE,255,720);
        //the button and text objects are added to the screen according to their x and y coordinates

        LogInt logint = new LogInt();
        addObject(logint,125,91);
        IntPlaceholder2 intplaceholder2 = new IntPlaceholder2();
        addObject(intplaceholder2,541,111);
        IntPlaceholder3 intplaceholder3 = new IntPlaceholder3();
        addObject(intplaceholder3,274,161);
        IntPlaceholder9 intplaceholder9 = new IntPlaceholder9();
        addObject(intplaceholder9,390,98);
        LogIntEqual logintequal = new LogIntEqual();
        addObject(logintequal,481,130);
        logint.setLocation(90,91);
        intplaceholder3.setLocation(235,156);
        intplaceholder9.setLocation(350,87);
        logintequal.setLocation(451,97);
        intplaceholder2.setLocation(537,109);
        intplaceholder9.setLocation(350,96);
        intplaceholder2.setLocation(538,99);
        logintequal.setLocation(447,97);
        Int2 int2 = new Int2();
        addObject(int2,83,285);
        Int3 int3 = new Int3();
        addObject(int3,262,289);
        Int9 int9 = new Int9();
        addObject(int9,427,283);
        
        logint.setLocation(98,82);
        
        
        IntInstructions intinstructions = new IntInstructions();
        addObject(intinstructions,314,357);
        logint.setLocation(106,73);
        
        logintequal.setLocation(448,76);
        intplaceholder2.setLocation(539,79);
        int2.setLocation(150,245);
        int3.setLocation(300,245);
        int9.setLocation(450,245);
        intinstructions.setLocation(307,346);
        
        intplaceholder9.setLocation(353,81);
        intplaceholder3.setLocation(246,137);
    }
    
    /**
     * This method will run the complete() method to check
     * if all answers are complete.
     */
    public void act(){
        if(int2.connected == true && int3.connected == true && int9.connected == true)
        {
          
           complete();
        }
    }
    
    /**
     * This method checks to see if all drag and drop answers have been snapped to the correct positions.
     * Once all have been done, a message is displayed.
     */
    public void complete(){
        if ((int2.connected == true && int2.correct == true) && (int3.connected == true && int3.correct == true) && (int9.connected == true && int9.correct == true))
        {
            //JOptionPane.showMessageDialog(null, "Well done. You have correctly completed the logarithm.");int2.connected = false;
            Help help = new Help();
            addObject(help, 512, 384);
            
            int2.connected = false;
            int3.connected = false;
            int9.connected = false;
        } else if (int2.correct == false || int3.correct == false || int9.correct == false) 
        {
            //JOptionPane.showMessageDialog(null, "Not all the answers are in the correct positions. Try again.");
            
            GreenfootImage image = new GreenfootImage("cactus.png");
            getBackground().drawImage(image,150, 360);
          
            if (reset == true){
                Greenfoot.setWorld(new LogarithmInteractive());   
            }
        }
    }
 
    }
danpost danpost

2017/3/14

#
Lines 14 through 16 create the required objects. You need not create more on lines 59, 61 and 63 (removes these lines). Same thing with lines 43, 45 and 47 (remove them).
You need to login to post a reply.