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

2015/6/4

Issue with Crab scenario

aag3981 aag3981

2015/6/4

#
I'm getting an error on the crab scenario when writing code to move the crab when a key is pressed. I wrote the command "turn(3);" and it's highlighting an error between the parenthesis and the semicolon. There is no space there. public void act() { move(4); if(Greenfoot.isKeyDown("left")) ( turn(3); ) if (Greenfoot.isKeyDown("right")) ( turn(-4); ) Any help would be apprecated!
davmac davmac

2015/6/4

#
In future please post the error message as it's often helpful in determining the cause of the error (even if you don't understand it yourself). In this case, it looks like you're using the wrong type of bracket. An if statement looks like:
    if (condition)
    {    // <-- curly bracket!

    }    // <-- curly bracket!
You are using round parentheses instead of curly braces.
aag3981 aag3981

2015/6/8

#
Thank you! I didn't see an error message. Just a place highlighted. I'll look out for it. Thanks so much again:)
davmac davmac

2015/6/8

#
No problem. The error message is displayed at the bottom of the editor window.
vivabenfica4 vivabenfica4

2015/6/8

#
You technically don't even have to use braces at all, but its good for organization. :)
plant plant

2015/6/10

#
Greenfoot does not care about the indentation. You could get rid of the brackets if there is just 1 code underneath the if statement in this case. IF there is more than 1 you need the brackets.
    public void act(boolean rweflying){
        boolean flying = rweflying;
        if (flying)
            flying=false;
        else 
            flying=true;
    }
You need to login to post a reply.