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

2014/11/18

Talking to Other Classes

meazalplaq meazalplaq

2014/11/18

#
Hello, I am making a point and click mouse game. It does not have any actors. The player uses the mouse like in the game Myst. I am trying to get one class to add and remove Objects when another class is called. For example, the players dies. The death screen appears, and the level starts over. When the level starts over, I would like a different dialog box to appear than the one before it. I am trying to use booleans to accomplish this. Am I on the right path? How do you check for true or false scenarios in Greenfoot?
The death scene class
-----------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Death_01 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Death_01 extends Actor
{
    int counter = 0;
    private static boolean deathDisplayed;
    
   public Death_01()
    {
        GreenfootImage myPic = new GreenfootImage(1024, 640);
        myPic.fill();
        myPic.drawImage(new GreenfootImage("Death_01.jpg"),0,0);

        setImage(myPic);
    }

    public void act() 
        {  
          
           counter++;  
          
           switchLocation();  
        }  
  
public void switchLocation()  
{  
    if (counter == 150) // the act method is called 30 times a second, 300 = 10 seconds.  
    {  
             
        setdeathDisplayed();

                   getWorld().setPaintOrder(Location_11.class);

                getWorld().removeObjects(getWorld().getObjects(Death_01.class));
                

               
                
    }  
}  

public static void setdeathDisplayed()  
{  
    deathDisplayed = false;  
  
}  
}
---------------------------------------
Current location class 
-----------------------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class building here.
 * 
 * @author Taylor Born
 * @version October 2009
 */
public class Location_11 extends Actor
{
//     int counter = 0;
   private boolean dialogDisplayed;  
   private static boolean deathDisplayed;
    
   
    public Location_11()
    {
        GreenfootImage myPic = new GreenfootImage(1024, 640);
        myPic.fill();
        myPic.drawImage(new GreenfootImage("Location_11.jpg"),0,0);

        setImage(myPic);
    }

    public void act() 
    {
    
    
    
      
 
         getdeathDisplayed();
          switchDialog_01();
         switchDialog_02();
         switchDialog_03(); 
         activeArea();

         area_Location_12();
        
        
    }   
 

public void switchDialog_01()  
{  

 if (!deathDisplayed&& !getWorld().getObjects(Button_01_off.class).isEmpty()&& !dialogDisplayed)
 {

       getWorld().addObject(new Dialog_08(), 307,481);
       
        dialogDisplayed = true;

        getWorld().setPaintOrder(Dialog_08.class); 
    }
// }
//                 
}

public void switchDialog_02()  
{  

 if (!deathDisplayed && getWorld().getObjects(Button_01_off.class).isEmpty()&& !dialogDisplayed)
 {

       getWorld().addObject(new Dialog_10(), 307,481);
       dialogDisplayed = true;
        getWorld().setPaintOrder(Dialog_10.class); 
    }
          
}

public void switchDialog_03()  
{   

 if (deathDisplayed && !getWorld().getObjects(Button_01_off.class).isEmpty()&& !dialogDisplayed)
 {

        getWorld().removeObjects(getWorld().getObjects(Dialog_08.class));

       getWorld().addObject(new Dialog_09(), 307,481);
       dialogDisplayed = true;
      
        getWorld().setPaintOrder(Dialog_09.class); 
    
             
}

}

public void activeArea()
{
  
   {
         MouseInfo mouse = Greenfoot.getMouseInfo();
        {

             if ( !getWorld().getObjects(Button_01_off.class).isEmpty()&& Greenfoot.mouseClicked(this) && mouse.getX() > 595 && mouse.getX() < 848 && mouse.getY() > 1 && mouse.getY() < 464)
               {
                  
                 
                  getWorld().addObject(new Death_01(), 512,320);
                  getWorld().setPaintOrder(Death_01.class);

                getWorld().removeObjects(getWorld().getObjects(Dialog_08.class));

            }
             else if ( getWorld().getObjects(Button_01_off.class).isEmpty() && Greenfoot.mouseClicked(this) && mouse.getX() > 595 && mouse.getX() < 848 && mouse.getY() > 1 && mouse.getY() < 464)
            {
              
                   getWorld().addObject(new Location_13(), 512,320);

                  getWorld().setPaintOrder(Location_13.class);
                  
                  
                getWorld().removeObjects(getWorld().getObjects(Dialog_08.class));
                getWorld().removeObjects(getWorld().getObjects(Dialog_10.class));
                getWorld().removeObjects(getWorld().getObjects(Location_11.class));
                
            }
        }
    } 
}

    
   public void area_Location_12()
   {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        {
    

              if (Greenfoot.mouseClicked(this) && mouse.getX() > 375 && mouse.getX() < 485 && mouse.getY() > 118 && mouse.getY() < 350)
               {
                  
                 
                  getWorld().addObject(new Location_12(), 512,320);
                  getWorld().setPaintOrder(Location_12.class);
                                 
                getWorld().removeObjects(getWorld().getObjects(Dialog_08.class));
                getWorld().removeObjects(getWorld().getObjects(Location_11.class));

            }
        }
    
    }
    
public static boolean getdeathDisplayed()  
{  
    return deathDisplayed;  
}             
}
------------------------
danpost danpost

2014/11/18

#
Strange -- you have a 'deathDisplayed' class field in both classes; yet, you have a 'get' method for the field in one and a 'set' method for the field in the other. Preferably, you should only have it in one class with both methods in that class. Then you can 'set' and 'get' the value of the field from anywhere using 'ClassNameFieldIsIn.methodName()' ('static' methods belong to the class, so you can, and should, use the class name to access them).
meazalplaq meazalplaq

2014/11/18

#
Thank you for your help. I will try this. Thanks,
You need to login to post a reply.