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

2013/10/3

Improving the Crab assignment - Please HELP!!!!!

snobiz45 snobiz45

2013/10/3

#
New to Greenfoot/Java coding. I keep getting this error message "class, interface or enum expected" - it highlights this code (public void checkKeypress () Here is my code: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * Act - do whatever the crab wants to do. * / public void act() { checkKeypress(); move (); lookForWorm (); } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { turn (-4); } if (Greenfoot.isKeyDown ("right")) { turn (4); } }
Gevater_Tod4711 Gevater_Tod4711

2013/10/3

#
I'm not shure why it highlights public void checkKeypressed() but the problem is that you can't write the code outside of a class, interface or enum. Java is completely object oriented so no code can be declared outside of classes (or interfaces or enums). So you have to surround your methods with a class declaration like this:
//first the imports:
import greenfoot.*;

//now the class declaration:
public class NameForYourClass extends Actor {

    //now your methods:
    public void act() {
        //...
    }

    //and also all the other methods or variables;

}//close the class declaration again.
The class is automaticly generated by greenfoots editor. You shouldn't delete it. That causes such errors.
snobiz45 snobiz45

2013/10/3

#
Thank you. I will give it a try!
snobiz45 snobiz45

2013/10/6

#
Gevater, It's still not working. I'm just not sure why.
danpost danpost

2013/10/6

#
Please show the class (the whole class from line one to the end), as it is now, and press Ctrl-I before posting it and use the 'code' tag below the reply-box to place the code into.
snobiz45 snobiz45

2013/10/7

#
New to Greenfoot/Java coding. I keep getting this error message "class, interface or enum expected" - it highlights this code (public void checkKeypress () Here is my code: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * Act - do whatever the crab wants to do. * / public void act() { checkKeypress(); move (); lookForWorm (); } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { turn (-4); } if (Greenfoot.isKeyDown ("right")) { turn (4); } }
Gevater_Tod4711 Gevater_Tod4711

2013/10/7

#
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) 

public class ClassName extends Actor
    /** 
      * Act - do whatever the crab wants to do. 
      */ 
    public void act() {
        checkKeypress(); 
        move (); 
        lookForWorm ();
    } 

    /** 
      * Check whether a control key on the keyboard has been pressed. 
      * If it has, react accordingly. */ 
    public void checkKeypress() {
        if (Greenfoot.isKeyDown("left")) { 
            turn (-4);
         } 
        if (Greenfoot.isKeyDown ("right")) {
            turn (4);
         }
    }
}
This code should work.
snobiz45 snobiz45

2013/10/10

#
Gevater, Can you help on this one. I keep getting an error message "can't find symbol - class lobster". Here is my code: import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorld extends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); } /** * Create the objects for the start of the game. */ public void CrabWorld() { addObject(new Crab(), 300, 300); addObject(new Lobster(), 300, 200); addObject(new Lobster(), 250, 500); addObject(new Lobster(), 320, 400); addObject(new Worm(), 20, 500); addObject(new Worm(), 30, 200); addObject(new Worm(), 60, 90); addObject(new Worm(), 80, 310); addObject(new Worm(), 150, 50); addObject(new Worm(), 210, 410); addObject(new Worm(), 220, 520); addObject(new Worm(), 380, 330); addObject(new Worm(), 410, 270); addObject(new Worm(), 530, 30); } } Thanks for any help you can offer. Sno
Gevater_Tod4711 Gevater_Tod4711

2013/10/10

#
You try to create new instances of the type Lobster (addObject(new Lobster(), ...); ) But you can only create this objects when the class called Lobster exists. In the class that represents your lobster you need to add the name of the class in the deklaration (public class Lobster extends Actor) If your class is called different or you haven't even got a class Lobster you need to change the name or create a new class. (To create a class you rightclick on the actor class, choose new subclass and enter the name Lobster)
You need to login to post a reply.