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

2013/3/22

Joy of Code#17

geekykid2013 geekykid2013

2013/3/22

#
Hi, I'm practicing a turtorial on the Joy of Code#17. When I paste the constructor public boolean atWorldEdge() into my code and compile I get an erro message : illegal start of expression. can you help? what have I done wrong? public class Ball extends Actor { /** * ACT METHOD - do whatever the Ball wants to do. This METHOD is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. move(4); //this is the move(); method from the ACTOR CLASS if (atWorldEdge()){ // atWorldEdge (); is a method from the ACTOR CLASS. We use the IF STATEMENT // when we are testing a condition. turn(153); // turn() is another method from the ACTOR CLASS that holds information in // parameter list in DEGREES } /** * TEST if we are close to one of the edges of the world. RETURN true if we are */ public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; } }
azazeel azazeel

2013/3/22

#
it's because you are missing close parentheses "}" after turn(153), put one more parentheses to close act method so the code will be like this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Ball extends Actor {
 /** * ACT METHOD - do whatever the Ball wants to do. This METHOD is called whenever 
 * the 'Act' or 'Run' button gets pressed in the environment. 
 */
	public void act() {
		move(4);
		if (atWorldEdge()){ 
		turn(153); 
		}
	} 

	public boolean atWorldEdge() {
		if(getX() < 10 || getX() > getWorld().getWidth() - 10)
			return true;
		if(getY() < 10 || getY() > getWorld().getHeight() - 10)
		return true;
		else return false; 
	}
}
azazeel azazeel

2013/3/22

#
ooh by the way, next time if you want to post source code, please click "code" link right below post /reply box
geekykid2013 geekykid2013

2013/3/22

#
ooh ok thanks bud
danpost danpost

2013/3/22

#
If you use the auto-indent feature in the Greenfoot application, you can then look at the end of the class code and see if you have too many or too few brackets (you should have exactly one line at the end whose first character is a closing squiggly bracket).
You need to login to post a reply.