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
danpost danpost

2013/11/17

#
Again, you cannot refer to the current world by creating a new one. Similar situation as above.
Kartoffelbrot Kartoffelbrot

2013/11/17

#
Ok now I understand nothing. What is this? I thought of something like:
    public class MyWorld extends TK2934World  
    {  
      
        Robot robot;

        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 = 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.setMoving(true);  
        }  
    }  
shern91 shern91

2013/11/17

#
Kartoffelbrot wrote...
Ok now I understand nothing. What is this? I thought of something like:
    public class MyWorld extends TK2934World  
    {  
      
        Robot robot;

        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 = 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.setMoving(true);  
        }  
    }  
i also don know how to explain to u..sorry..this project already include some sub class like "Tile,MessageBar and Timer"..so the question request i doing step by step.. at the end robot will no move longer it will control by client using clickedAct()
Kartoffelbrot Kartoffelbrot

2013/11/17

#
I meant this:
shern91 wrote...
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(); } }
The code I posted was an improvement.
shern91 shern91

2013/11/17

#
Kartoffelbrot wrote...
I meant this:
shern91 wrote...
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(); } }
The code I posted was an improvement.
OK...in the clickedAct() method of the MoveRightClass, add a statement which sends a moveRobot() message to the Robot object's world.
Kartoffelbrot Kartoffelbrot

2013/11/17

#
Ok, then read danpost's last comment.
shern91 shern91

2013/11/17

#
Kartoffelbrot wrote...
Ok, then read danpost's last comment.
i did it. i follow yours code(MyWorld)
public void moveRobot(MoveRightTile tile)
    {
        robot.setMoving(true);
    }

and in my Class MoveRightTile
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.
     */
    public MoveRightTile()
    {
        
    }
    
    public void clickedAct()
    {
     Tile tile;
     MyWorld my = (MyWorld)getWorld();
     my.moveRobot(this);
    }
    
now i want ot add lefttile in Tile,and will control the robot move left and right

}
Kartoffelbrot Kartoffelbrot

2013/11/17

#
Watch the Joy of code. It will explain more, than our words: http://www.youtube.com/user/18km
shern91 shern91

2013/11/17

#
Kartoffelbrot wrote...
Watch the Joy of code. It will explain more, than our words: http://www.youtube.com/user/18km
than you but my hostel wifi speed is slow like turtle...i will try to watch next time..
You need to login to post a reply.
1
2