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

2022/10/28

Illegal start of expression

Ilovemarcus Ilovemarcus

2022/10/28

#
im trying to make it so that it adds the 3 things randomly and while i was doing the prepare() method it kept saying illegal start of expression and idk what i am doing wrong. Im kind of new to java.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(800, 600, 1); 
        showText("Use the arrow keys to move", 400 , 200);
        showText("Collect 50 balloons or 10 fish to win", 400 , 250);
        showText("If you hit a bomb you lose", 400 , 300);
        
        if (Greenfoot.isKeyDown("s"))
        {
            act();
            //prepare();
        }
        
        private void prepare()
        {
            for(int i = 0; i < 70; i++)
            {
                //Got the int x and int y from a youtube video by BRaditechUK
                int x = Greenfoot.getRandomNumber(getWidth() - 1);
                int y = Greenfoot.getRandomNumber(getHidth() - 1);
                addObject(new Balloon(), x, y);
            }
            for(int i = 0; i < 25; i++)
            {
                int x = Greenfoot.getRandomNumber(getWidth() - 1);
                int y = Greenfoot.getRandomNumber(getHidth() - 1);
                addObject(new fish(), x, y);
            }
            for(int i = 0; i < 15; i++)
            {
                int x = Greenfoot.getRandomNumber(getWidth() - 1);
                int y = Greenfoot.getRandomNumber(getHidth() - 1);
                addObject(new Bomb(), x, y);
            }
        
            
        }
        
    }
danpost danpost

2022/10/28

#
Ilovemarcus wrote...
im trying to make it so that it adds the 3 things randomly and while i was doing the prepare() method it kept saying illegal start of expression and idk what i am doing wrong. Im kind of new to java. << Code Omitted >>
There are several things to be fixed. Here is what I found: (1) lines 23 through 27 are misplaced; (2) prepare method starts before constructor block ends (constructor needs to close before a new method is introduced); (3) random y values use the wrong world dimension; For (1), remove those lines for now (lines 23 - 27). For (2), add the following code where those lines were:
    prepare();
}
For (3), change getWidth in lines 35, 41 and 47 with getHeight. As far as the removed code, it would help to know what you were trying to do with that code. Checking on a key being down is something that can only be done in an active world. A world cannot become active until it is fully created (after constructor and prepare methods are completed and the world is set active). The act method is automatically called by greenfoot, plus, you have not given the act method any implementation for the world (no overriding act method is coded in your world).
You need to login to post a reply.