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

2016/1/8

Starting Game

ironphoenix20 ironphoenix20

2016/1/8

#
Here is my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Start here. * * @author (your name) * @version (a version number or a date) */ public class Start extends World { /** * Constructor for objects of class Start. * */ public Start() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(700, 600, 1); GreenfootImage ghosttown = new GreenfootImage("Ghost Town.jpg"); ghosttown.scale(getWidth(), getHeight()); setBackground(ghosttown); GameStartCard start= new GameStartCard(); addObject(start, 350, 300); if (Greenfoot.isKeyDown("space")) { Greenfoot.setWorld(new Background()); } } } I want greenfoot to load a new world if the user presses space, as you can see in the code. But this is not working. Can you help me fix this?
danpost danpost

2016/1/8

#
You have one block of code in the class -- the 'public Start' constructor block. This code is executed once -- when you reset (or compile) the project and a new Start object is created. Once the Start object is created, space detection will not take place (as the code is now). As a general rule, never place mouse or keyboard detection code in the constructor of a class.
You need to login to post a reply.