It is possible you have an infinite loop. Please show all codes that should execute during world creation.
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
}
Okay. You have two constructors that execute when you create a new world, Barrel and Plane. Show these constructors.
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
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
It's odd, but has happened more than a few times when I've been using the new version
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.
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
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?
Yep, still says there are no errors.
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