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

2013/11/16

How to send message to another class

1
2
shern91 shern91

2013/11/16

#
public class MyWorld extends TK2934World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
       
        super(750, 580);
        displayTimer();
        resetTimer(100);
        startTimer();
        MoveRightTile r = new MoveRightTile();
        addObject(r,500,300);
        Robot robot = new Robot();
        addObject(robot,50,100);
        //Robot.setMoving(false);
        
    }   

public class Robot extends Runner
{
    /**
     * Act - do whatever the Robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    boolean moving=true;
    public Robot() 
    {
        //super();
        int dx=5;
        int dy=0;
        setDirection(dx,dy);
    }
    
    public void runningAct()
    {
        //super.runningAct();
        if(moving)
        {
           super.runningAct(); 
        }
    }
    public boolean setMoving()
    {
        //moving = test;
        return true;
    }
}
The question is: 1. add a method named setMoving() which accepts a boolean parameter. This method set the moving attribute to the value of that parameter. 2.Modify the runningAct() method in class Robot so that it call its parent's runningAct() method ONLY IF the robot object is currently moving. 3.In the MyWorld consturctor send the message setMoving(false) to the Robot object after its creation in that method.
Kartoffelbrot Kartoffelbrot

2013/11/16

#
Is that what you mean in 1?
public void setMoving(boolean moveAllowed){
moving = moveAllowed;
}
I think you made 2 and 3 so far.
shern91 shern91

2013/11/16

#
i think should be public boolean setMoving(boolean moveAllowed) { moving = moveAllowed; } because the questin requst add a method named setMoving() which accepts a boolean parameter
shern91 shern91

2013/11/16

#
4. Modift the runningAct() method in step 10 of this stage so that the Robot object is set to non-moving after calling the inherited after calling the inherited ruuningAct() method... how do i set to non-moving after calling the inherited
Kartoffelbrot Kartoffelbrot

2013/11/16

#
And what do you want to return in setMoving? And I think accepting a boolean parameter means setMoving(boolean moveAllowed) because boolean moveAllowed is a parameter. The boolean in public boolean setMoving() requsts a return statement, not a parameter. And here I see no value that has sense to be returned. 4:
public void runningAct()  
    {  
        if(moving)  
        {  
           super.runningAct();
           moving = false;
        }  
    }  
shern91 shern91

2013/11/16

#
thank you very much...i did it...
shern91 shern91

2013/11/16

#
now the question ask me create a new method in MyWorld and name it moveRobot(),i created. like below.
public class MyWorld extends TK2934World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
       
        super(750, 580);
        displayTimer();
        resetTimer(300);
        startTimer();
        MoveRightTile r = new MoveRightTile();
        addObject(r,500,300);
        MoveLeftTile l = new MoveLeftTile();
        addObject(l,400,300);
        Robot robot = new Robot();
        addObject(robot,50,100);
        robot.setMoving(false);
        
    }   
       public void world_act()
       {
        if(timesUp())
        {
            displayMessage("Time's Up",Color.red);
        }
        
    }
    public void moveRobot()
    {
        Robot robot2 = new Robot();
        robot2.setMoving(true);
    }
}
and set the message setMoving() to false. In the clickedAct() method of the MoveRightTile class, put a statement which sends a moveRobot() message to the Robot object's world
public class MoveRightTile extends Tile
{   
    public void clickedAct()
    {
      Robot robot = new Robot();
      robot.setMoving(true);
    }
}
how to send the moveRobot() message to Robot object's world?
shern91 shern91

2013/11/17

#
i tried many time like
public class MoveRightTile extends Tile  
{     
    public void clickedAct()  
    {  
      Robot robot = new Robot();  
      robot.setMoving(true);  
    }  
}  
but did not success...
Kartoffelbrot Kartoffelbrot

2013/11/17

#
I think the problem is, that you create a new Robot in every method. You never place them into the world, so they get deleted after using the method. You have to save one robot as a member variable (write this outside of a method: Robot robot = new Robot();). And then you don't have to create a new robot every time, which is useless, if I understood it correctly.
shern91 shern91

2013/11/17

#
Kartoffelbrot wrote...
I think the problem is, that you create a new Robot in every method. You never place them into the world, so they get deleted after using the method. You have to save one robot as a member variable (write this outside of a method: Robot robot = new Robot();). And then you don't have to create a new robot every time, which is useless, if I understood it correctly.
create a Robot robot = new Robot(); at outside constructor MyWorld?
danpost danpost

2013/11/17

#
Of course, you did not succeed. You are creating a NEW robot, which is not added into the world and therefore you will not see any change in it. At any rate, it is not the same robot as that which you do have in your world. To change line 5 so that 'robot' refers to the one already in your world, you need to use the World class method 'getObjects' which returns a list object, followed by the List class method 'get' (refer to List class API) which returns an element from that list. That element (which is typed as an Object object -- because 'getObjects' creates a list of Object objects) must be typecast to a Robot object (refer to the last section of this Java tutorial page) before the 'setMoving' method can be applied to it.
Kartoffelbrot Kartoffelbrot

2013/11/17

#
Do you want to have many robots, or only one?
shern91 shern91

2013/11/17

#
Kartoffelbrot wrote...
Do you want to have many robots, or only one?
only one,haha
shern91 shern91

2013/11/17

#
danpost wrote...
Of course, you did not succeed. You are creating a NEW robot, which is not added into the world and therefore you will not see any change in it. At any rate, it is not the same robot as that which you do have in your world. To change line 5 so that 'robot' refers to the one already in your world, you need to use the World class method 'getObjects' which returns a list object, followed by the List class method 'get' (refer to List class API) which returns an element from that list. That element (which is typed as an Object object -- because 'getObjects' creates a list of Object objects) must be typecast to a Robot object (refer to the last section of this Java tutorial page) before the 'setMoving' method can be applied to it.
thank you...i trying now..
shern91 shern91

2013/11/17

#
actually i upload wrong code
public class MoveRightTile extends Tile
{
    /**
     * Act - do whatever the MoveRightTile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //MyWorld w = new MyWorld();
    public MoveRightTile()
    {
        
    }
    
    public void clickedAct()
    {
     MyWorld my = new MyWorld();
     my.moveRobot();
    }
}
this is my code
There are more replies on the next page.
1
2