If the only condition to start the game is that a selection is made, then you can put the code to start the game in the 'setCharselect' method.
{if (Greenfoot.isKeyDown("whatever key this is"))
Greenfoot.setWorld(new LB_HeavenWorld());
}java.lang.ClassCastException: SelectionWorld cannot be cast to L4_SpaceWorld at ActorName.selected(Seulgi.java:20) at ActorName.act(Seulgi.java:53)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Irene here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Irene extends Animal
{
public Irene()
{
GreenfootImage image = getImage();
image.scale(image.getWidth() - 100, image.getHeight() -100);
setImage(image);
}
/**
* Act - do whatever the Irene wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (canSee(SuperCandy.class))
{
eat(SuperCandy.class);
}
if (Greenfoot.isKeyDown("up"))
{
move(10);
}
if (Greenfoot.isKeyDown("down"))
{
move(-5);
}
if (Greenfoot.isKeyDown("left"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("right"))
{
turn(5);
}
{
if (Greenfoot.mouseClicked(this)) ((L4_SpaceWorld)getWorld()).setCharselect(1);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SelectionWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SelectionWorld extends World
{
private int Charselect;
public int setCharselect(int x)
{
Charselect = x;
return Charselect;
}
public int getCharselect()
{
return Charselect;
}
/**
* Constructor for objects of class SelectionWorld.
*
*/
public SelectionWorld()
{
//Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(560, 560, 1);
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
setPaintOrder(SelectionWorld.class, L4_SpaceWorld.class, LB_HeavenWorld.class);
addObject(new Irene(), 1, 1);
addObject(new Seulgi(), 1, 1);
addObject(new Wendy(), 1, 1);
addObject(new Joy(), 1, 1);
addObject(new Taeyeon(), 1, 1);
}
}
the code for the space world is then:
import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)
public class L4_SpaceWorld extends World
{
private CountdownClock timer;
private GreenfootSound backgroundMusic = new GreenfootSound("행복 Happiness.mp3");
private int Charselect;
public int setCharselect(int x)
{
Charselect = x;
return Charselect;
}
public int getCharselect()
{
return Charselect;
}
/**
* Create the space world (the universe). Our world has a size
* of 900x600 cells, where every cell is just 1 pixel.
*/
public L4_SpaceWorld()
{
super(560, 560, 1);
//etc. backgroundMusic.playLoop();
prepare();
setPaintOrder(CountdownClock.class, Irene.class, Seulgi.class, Wendy.class, Joy.class, Taeyeon.class,
SuperCandy.class);
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
timer = new CountdownClock(60);
addObject(timer, 60, 45);
addRandomSuperCandy(100);
}
private void addRandomSuperCandy(int howMany)
{
int i;
for (i=0; i < howMany; i++)
{
addObject(new SuperCandy(), 200 + Greenfoot.getRandomNumber(getWidth() - 200),
100 + Greenfoot.getRandomNumber(getHeight()) - 200);
}
}
public void act()
{
if (timer.getCounter() <= 0)
{
Greenfoot.stop();
}
{if (Greenfoot.isKeyDown("A"))
Greenfoot.setWorld(new LB_HeavenWorld());
}
if (timer.getCounter() <= 0)
{
gameOver("Oh no! You ran out of time!");
}
java.util.List SuperCandys = getObjects(SuperCandy.class);
if (SuperCandys.isEmpty())
{
gameOver("You win\nAll candies eaten. Press 'A' to go to the next level.");
}
}
public void gameOver(String msg)
{
PopUpMessage popup = new PopUpMessage(msg);
addObject(popup, getWidth() / 2, getHeight() / 2);
Greenfoot.stop();
}
}
import greenfoot.*;
public class Selector extends Actor
{
Actor selection;
public Selector(Actor actor)
{
selection = actor;
GreenfootImage img = new GreenfootImage(actor.getImage());
GreenfootImage text = new GreenfootImage(actor.getClass().getName(), 24, null, null)
GreenfootImage image = new GreenfootImage(80, 100);
img.scale(80, 80);
image.drawImage(img, 0, 0);
image.drawImage(text, 40-text.getWidth()/2, 76);
setImage(image);
}
public void act()
{
if (Greenfoot.mouseClicked(this))
{
L4_SpaceWorld spaceWorld = new L4_SpaceWorld();
spaceWorld.setSelection(selection);
Greenfoot.setWorld(spaceWorld);
}
}
}public SelectionWorld()
{
super(560, 560, 1);
prepare();
}private void prepare()
{
addObject(new Selector(new Irene()), 60, 300);
addObject(new Selector(new Seulgi()), 170, 300);
addObject(new Selector(new Wendy()), 280, 300);
addObject(new Selector(new Joy()), 390, 300);
addObject(new Selector(new Taeyeon()), 500, 300);
} if (canSee(SuperCandy.class))
{
eat(SuperCandy.class);
}
private void prepare()
{
addObject(new blablabla)
} GreenfootImage image = new GreenfootImage(80, 100);
img.scale(80, 80);
image.drawImage(img, 0, 0);