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

2012/3/8

Code breaker

SBaker SBaker

2012/3/8

#
I am trying to run the code breaker with my students and I am finding Greenfoot very difficult. I have this piece of code: public void act() { // Add your action code here. if (Greenfoot.isKeyDown("up")); { move(5); } if (Greenfoot.isKeyDown("down")); { move(-5); } } in the Ship class but it does not work. It compiles. Just does nothing. I am finding it difficult to promote greenfoot in my school when so many things don't work. It has taken about three sessions just to get the latest version to compile. This is basic code and I can't find any help for it at all. Sophie Baker
mjrb4 mjrb4

2012/3/8

#
Hi Sophie, You've fallen for a classic one here - the if statements shouldn't have semi-colons after them. If they do, like they do here, then Java compiles them as "empty" statements - i.e. "if(condition) {}". The braces afterwards just denote a separate block of code. So what you actually have here since the if statements are discounted, is:
move(5);
move(-5);
...hence overall nothing happens! If you remove the semi-colons from the end of the if statements, it should work as you expect. At some point, we should probably put warnings in to catch this sort of thing... If you're a teacher, you may also find the Greenroom useful (http://greenroom.greenfoot.org) - it's specifically geared at teachers making use of Greenfoot.
SBaker SBaker

2012/3/9

#
Thanks for this. It all works now. I don't feel however that I have fallen for anything other than copying directly from the codebreaker materials. Would it bepossiblefor someone to review the Codebtraker materials and correct the errors because it is impossible to enthuse students about an enviropnment that never seems to work because of errors in the materials. Regards Sophie
mjrb4 mjrb4

2012/3/9

#
Hi Sophie, I'm assuming you mean the set of examples you can download from this page? I've taken a quick look through and in all the provided Ship.java files (3 as far as I can tell) I haven't seen that mistake made. Where exactly did you find the erroneous code in the materials?
davmac davmac

2012/3/9

#
Or, if there are problems with other files, could you tell us which ones specifically? Thanks!
You need to login to post a reply.