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

2016/6/6

title screen

benbec1 benbec1

2016/6/6

#
I want to change my title image to the main world image when E is pressed. When I compile there are no syntax errors but when I run it doesnt work. Here is the code:
import greenfoot.*;
import java.awt.Color;

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

    /**
     * Constructor for objects of class world.
     * 
     */
    public world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 500, 1); 
        setBackground(new GreenfootImage("Title screen.png"));
        if(Greenfoot.isKeyDown("e"))
            setBackground(new GreenfootImage("moze.png"));

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Mazer mazer = new Mazer();
        if(Greenfoot.isKeyDown("e"))
        {addObject(mazer, 1, 1);
            mazer.setLocation(240, 13);}
        Griever griever = new Griever();
        if(Greenfoot.isKeyDown("e"))
            addObject(griever, 15, 18);
    }
}
SPower SPower

2016/6/6

#
You have placed the code to check key presses in the constructor, which only gets executed once, namely when the world is instantiated. You need to move the code to the world's act method. And as programming advice: classnames shouldn't start with a lowercase, but with an Uppercase letter. So I would advice you to change the name of 'world' into something like 'MyWorld', and edit the name of the constructor appropriately.
benbec1 benbec1

2016/6/6

#
Thank you so much my game works now
You need to login to post a reply.