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

2016/9/3

waiting method

NIRO17 NIRO17

2016/9/3

#
I am developing a scenario with two robots movements.i want to wait one robot until other robot finish it's opertion. how can i wait it.. my code is here

public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
    
    private void prepare(){
      
     
      man1 man1=new man1();
      int manX1=Greenfoot.getRandomNumber(getWidth());
      int manY2=Greenfoot.getRandomNumber(getHeight());
      addObject(man1, manX1, manY2);
      
      man2 man2=new man2();
      int manX12=Greenfoot.getRandomNumber(getWidth());
      int manY21=Greenfoot.getRandomNumber(getHeight());
      addObject(man2, manX12, manY21);
      
      Barrel[] barel=new Barrel[1];
      for(int i=0;i<barel.length;i++){
       barel[i]=new Barrel();
       int manX=Greenfoot.getRandomNumber(getWidth());
       int manY=Greenfoot.getRandomNumber(getHeight());
       addObject(barel[i],manX,manY);
        }
}}


public class man1 extends Actor
{
    /**
     * Act - do whatever the man1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAround(); // Add your action code here.
    }    
    
      public void moveAround(){
            
            move(4);
            if(Greenfoot.getRandomNumber(100)<10){
                turn(Greenfoot.getRandomNumber(180)-90);
            }
        }
}

public class man2 extends Actor
{
    /**
     * Act - do whatever the man2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       moveAround(); // Add your action code here.
    }  
    
      public void moveAround(){
            
            move(2);
            
            if(Greenfoot.getRandomNumber(100)<10){
                turn(Greenfoot.getRandomNumber(180)-90);
            }
        }
}


danpost danpost

2016/9/3

#
NIRO17 wrote...
I am developing a scenario with two robots movements.i want to wait one robot until other robot finish it's opertion. how can i wait it
You could do it by adding boolean fields in the actor classes to indicate their active states; but is seems to me that this is something that the World object should deal with. There are three phases here (a) the first robot performs its task (b) the second robot performs its task and (c) both have completed there tasks. Therefore and int field to indicate which phase is currently being executed would be in order:
private int phase;

//in MyWorld class
public void act() // phase operations
{
    switch (phase)
    {
        case 0:  man1.operate(); break;
        case 1:  man2.operate(); break;
        case 2:  Greenfoot.stop(); break;
    }
}

public void nextPhase() // changing phase
{
    phase++;
}
Now, you will need to rename the 'act' methods of the man1 and man2 classes to 'operate' (so greenfoot no longer calls them automatically and the switch statement can control their executions). Also, you will need to check in the man1 and man2 classes for their tasks to be completed and, when they are found to be, call the 'nextPhase' method of the MyWorld class:
((MyWorld)getWorld()).nextPhase();
I forgot to mention, you will need to keep references to your man1 and man2 objects in your MyWorld class:
private man1 man1;
private man2 man2;

// creating them with
man1 = new man1();
// and
man2 = new man2();
You need to login to post a reply.