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

2019/5/25

Why is this an illegal expression?

AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/25

#
I managed to work out one bit of code, but then I have a new error lol. the game says that public Plane is an illegal start of expression. Here's the code:
import greenfoot.*;

/**
 * Write a description of class Plane here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Plane extends Actor
{
    /**
     * Act - do whatever the Plane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     * if (isAtEdge())
    {
        Greenfoot.setWorld(new SkyEnemy()); or Greenfoot.setWorld(new SkyEnemyTwo()); or Greenfoot.setWorld(new SkyEnemyThree());
    }
     */
    public void act() 
    {
    {
            setRotation(0);
            move(2);
    }    
        if (Greenfoot.isKeyDown("Up"))
    {
            setRotation(-20);
            move(2);
    }
    if (Greenfoot.isKeyDown("Down"))
    {
        setRotation(70);
            move(2);
        }
    if (isAtEdge())
    {
        if (Greenfoot.getRandomNumber(4)==1)
        {
            Greenfoot.setWorld(new SkyEnemy());
    }
       if (Greenfoot.getRandomNumber(4)==2)
       {
           Greenfoot.setWorld(new SkyEnemyTwo());
        }
        if (Greenfoot.getRandomNumber(4)==3)
        {
            Greenfoot.setWorld(new SkyEnemyThree());
        }
        public Plane()
{
    GreenfootImage image = getImage();
    image.scale(image.getWidth() - 80, image.getHeight() - 80);
    setImage(image);
}
    }
    }
}
AdiBak AdiBak

2019/5/25

#
The constructor has to be the first thing that you write in your code (just above the act() method), since it is called when an object is instantiated.
Super_Hippo Super_Hippo

2019/5/25

#
The order does not matter. (At least not for the error message. It is just normal the constructor is placed at the top so it is easy to find.) What matters is that the method should not be in another method. Right now, the constructor is inside the act method and that doesn't work.
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/25

#
Thanks, it works now!
You need to login to post a reply.