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

2012/2/19

Class, interface, or enum expected

1
2
DMag DMag

2012/2/19

#
I've just started with greenfoot and I'm following the tutorial. At one point it says to add:
    /**

     * Turn in a random direction.

     */

    public void turnRandom()

    {

        // get a random number between 0 and 3...

        int turns = Greenfoot.getRandomNumber(4);

        

        // ...an turn left that many times.

        for(int i=0; i<turns; i++) {

            turnLeft();

        }

    }
But when I do it gives me a class, interface, or enum expected error. I don't see any out of place brackets, but I am new so I could be missing something. And can someone explain what "public void" means? Thanks!
Game/maniac Game/maniac

2012/2/19

#
Public means your method can be used by the object/world and it's subclasses. Void is the type of method in this case the method will run the code in order from top to bottom once until called up again
DMag DMag

2012/2/19

#
Oh I see. So does private mean that it will run but can't be referenced outside of the operation (if that's what it's called)
Game/maniac Game/maniac

2012/2/19

#
You can't do turnLeft(); because greenfoot doesn't know turnLeft(); to fix this do turn(-int); int is how much you want the actor to turn left, as an example turn(-5);
Game/maniac Game/maniac

2012/2/19

#
Yes
Game/maniac Game/maniac

2012/2/19

#
Int = value
DMag DMag

2012/2/19

#
I just did that, but when it gives the error it highlights "public void turnRandom() "
Game/maniac Game/maniac

2012/2/19

#
You might be able to get away by just doing Public void randomturn() { turn(Greenfoot.getRandomNumber(-3)); }
Game/maniac Game/maniac

2012/2/19

#
It does the same thing
DMag DMag

2012/2/19

#
Nope, same error
Game/maniac Game/maniac

2012/2/19

#
Are you typing code into the subclass of an actor
DMag DMag

2012/2/19

#
I think so, there's a box that says actors and a box with wombat pointing to it, I right click on the wombat box and hit open editor.
Game/maniac Game/maniac

2012/2/19

#
Does your code look like this public void act() { public void turnRandom() { turn(Greenfoot.getRandomNumber(3)); } }
davmac davmac

2012/2/19

#
But when I do it gives me a class, interface, or enum expected error.
Your method is most likely either inside another method, or outside the class altogether. Eg:
class X {
  ...
}

// This is outside the class!
public void turnRandom()
{
    ...
}
DMag DMag

2012/2/19

#
Davmac, I think that was the problem. It came up with a different error and I tried to edit some things and I completely messed up the wombat's code, so I'm trying to get the original back now.
There are more replies on the next page.
1
2