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

2011/12/2

Diagonal

Steve_888 Steve_888

2011/12/2

#
Hi guys. Im guessing this is just something to do with the syntax or im missing something of some sort but why does the robot for the following code go diagonal instead of following the steps after the if statement? How would i make the robot follow each command instead? Thanks.
public void act()
    {
        if(endItNow()==true)
       {
           Greenfoot.stop();
        }
        else
        {
            int x = getX();
            int y = getY();
            setLocation(x+1,y);
            if(checkBumpers()==true)
            setLocation(x, y-1);
    >      setLocation(x + 1, y);
    >      setLocation(getX(), getY() - 1);
            
            
        }
    
       
    }
mjrb4 mjrb4

2011/12/2

#
Do you want all the code after the if statement to happen in the if statement? If so you need to use curly braces like so:
if(checkBumpers()==true)  {
     setLocation(x, y-1);  
     setLocation(x + 1, y);  
     setLocation(getX(), getY() - 1);  
}
...Otherwise just the line after the if statement will be included (but it's good practice to use these braces all the time so people can see what you mean, even if it is just one line you're after.)
Steve_888 Steve_888

2011/12/2

#
Ok thankyou i shall try that. Just out of curiousity, is there any way the robot can be coded so that however many it goes up it must come down e.g if it goes up 2 it comes down 2 or goes up 4 it comes down 4. As its dodging obstacles in the 'world' there may be a situation where 3 or 4 obstacles may be on top of each other and so ideally, a code that can deal with that would be good. Is there a particular method for that?
Morran Morran

2011/12/2

#
You could have an instance variable "howFarIveMovedUp" that gets incremented each time the robot moves up, and when the robot wants to move down again, each time it moves down it decrements "howFarIveMovedUp". Once "howFarIveMovedUp" gets to zero, it knows to stop going back down.
Steve_888 Steve_888

2011/12/2

#
Ive heard about instance variables and that sounds like a good plan to me. How would i write that in Java code though? Thanks.
Morran Morran

2011/12/2

#
For your class, write:
class whatever extends Actor
{
   //your class here

   private int howFarIveMovedUp;
   private int whateverOtherInstanceVariableYouWant;
   //and if you want a decimal number, you do it like this
   private double decimalInstanceVariable;
   //and if you want a variable that holds letters, you do it like this
   private String characterHoldingInstanceVariable;
}
The variable names are entirely up to you. However, it is good practice to name them clear and concise names, even if it takes longer to type them(abbreviations are okay, though).
Steve_888 Steve_888

2011/12/2

#
Thanks Morran. I shall look into that. @mjrb4 I tried the curly braces round the if statement but it went diagonally top right instead when it met the object as if it merged the code together. My goal is to make the robot see the object (which it does), go up one, go forward 2 (to clear the obstacle) and then go back down one to get back to the original path. What would make it read each statement separately as in go up - then - go forward - then - go down?
kiarocks kiarocks

2011/12/3

#
maybe you want Greenfoot.delay(int)
You need to login to post a reply.