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

2019/3/2

World Not Instantiating

frapples frapples

2019/3/2

#
I have had issues with the new version of Greenfoot where it will not instantiate the world. It just sits with an "Instantiate a new World object" message and there seems to be no way of bringing it back to life. All the classes compile, is this a known issue?
danpost danpost

2019/3/2

#
It is possible you have an infinite loop. Please show all codes that should execute during world creation.
frapples frapples

2019/3/3

#
Here is my code, no infinite loop. Happens quite a lot on the new version. import greenfoot.*; /** * Write a description of class BarrelWorld here. * * @author (your name) * @version (a version number or a date) */ public class BarrelWorld extends World { private int score = 0; private int lives = 3; /** * Constructor for objects of class BarrelWorld. * */ public BarrelWorld() { // Create a new world with 800x600 cells with a cell size of 1x1 pixels. super(800, 600, 1); addObject(new Barrel(250),66,72); addObject(new Plane(),400,300); }//end constructor public void removeLife() { lives --; showText("Lives:"+lives,60,570); if (lives == 0) { Greenfoot.stop(); }//endif }//end method removeLife public void updateScore() { score++; showText("Score:"+score,60,585); }//end method updateScore }
danpost danpost

2019/3/3

#
Okay. You have two constructors that execute when you create a new world, Barrel and Plane. Show these constructors.
frapples frapples

2019/3/3

#
Here is the Barrel one: import greenfoot.*; /** * Write a description of class Barrel here. * * @author (your name) * @version (a version number or a date) */ public class Barrel extends Actor { private int maxtime; private int timer; public Barrel(int maxtime
frapples frapples

2019/3/3

#
Here is the Plane one: import greenfoot.*; /** * Write a description of class Plane here. * * @author (your name) * @version (a version number or a date) */ public class Plane extends Actor { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; private boolean isGoingFast; public Plane(){ image1 = new GreenfootImage("airplane1.png"); image2 = new GreenfootImage("airplane2.png"); image3 = new GreenfootImage("airplaneFaster.png"); isGoingFast = false; }//end constructor /** * Act - do whatever the Plane wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { handleMovement(); rocketCollision(); grabBarrel(); animate(); } //end method act public void animate() { if (getImage() == image1 && !isGoingFast) { setImage(image2); } else if (!isGoingFast) { setImage(image1); }//endif }//end method animate private void grabBarrel() { if (isTouching(Barrel.class)) { removeTouching(Barrel.class); BarrelWorld world = (BarrelWorld)getWorld(); world.updateScore(); getWorld().addObject(new Barrel(250),Greenfoot.getRandomNumber(780)+10, Greenfoot.getRandomNumber(580)+10); }//endif }//end method grabBarrel private void rocketCollision() { if (isTouching(Rocket.class)) { setLocation(400,300); Greenfoot.playSound("Bang.wav"); BarrelWorld world = (BarrelWorld)getWorld(); world.removeLife(); }//endif }//end method rocketCollision private void handleMovement() { if (Greenfoot.isKeyDown("left")) { turn(-2); } else if (Greenfoot.isKeyDown("right")) { turn(2); }//endif if (Greenfoot.isKeyDown("up")) { move(3); isGoingFast = true; setImage(image3); } else { move(2); isGoingFast = false; }//endif }//end method handleMovement }//end class Plane
frapples frapples

2019/3/3

#
It's odd, but has happened more than a few times when I've been using the new version
frapples frapples

2019/3/3

#
Did happen once when I had an infinite loop, but even when I corrected the error the world still wouldn't come back to life.
danpost danpost

2019/3/3

#
frapples wrote...
Here is the Barrel one: << Code Omitted >>
Incomplete post.
frapples frapples

2019/3/3

#
import greenfoot.*; /** * Write a description of class Barrel here. * * @author (your name) * @version (a version number or a date) */ public class Barrel extends Actor { private int maxtime; private int timer; public Barrel(int maxtime ){ this.maxtime = maxtime-Greenfoot.getRandomNumber(50); timer = 0; }//end constructor /** * Act - do whatever the Barrel wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { resetBarrel(); }//end method act public void resetBarrel() { timer++; if (timer > maxtime) { timer = 0; setLocation(Greenfoot.getRandomNumber(780)+10,Greenfoot.getRandomNumber(580)+10); }//endif }//end method resetBarrel }//end class Barrel
danpost danpost

2019/3/3

#
In your BarrelWorld class editor, go to the end of the first line and press "enter", then "backspace". After, does it say "Class compiled -- no syntax errors" at the bottom?
frapples frapples

2019/3/3

#
Yep, still says there are no errors.
danpost danpost

2019/3/3

#
frapples wrote...
Yep, still says there are no errors.
Try the same with your other classes. Then try closing greenfoot and opening it back up. Then try re-installing greenfoot. Then revert to an older version of greenfoot.
frapples frapples

2019/3/3

#
Not sure how viable this makes Greenfoot in a classroom environment. If a student makes a mistake and Greenfoot can't recover from it even when they have corrected the code, that's a huge problem. Thanks for your help
You need to login to post a reply.