There is "world" and "puzzleIntro" under "World" in my project.
In "world" it defines an actor, and then creates a "button" with a parameter.
In "button" it assigns the parameter to the givenName.
When clicked, it the "buttonCommand" method present in "world", send the button's "name"
BACK in "world", it receives the string "Start" for use in the buttonCommand
This worked last time I used Greenfoot, but today when I click the "start" button, it says this error:
java.lang.ClassCastException: world cannot be cast to puzzleIntro
at button.sendCommand(button.java:28)
at button.checkClicked(button.java:39)
at button.act(button.java:23)
at greenfoot.core.Simulation.actActor(Simulation.java:507)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:470)
at greenfoot.core.Simulation.runContent(Simulation.java:204)
at greenfoot.core.Simulation.run(Simulation.java:194)
Any ideas?
button button1 = new button("Start"); public world() { super(870, 500, 1); addObject (button1, 435, 350); }
private String name; public button(String givenName) { name = givenName; setImage(name + ".png"); //Setting the right image }
public void act() { checkClicked(); } public void checkClicked() { Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) { sendCommand(); } } public void sendCommand() { world world = (world) getWorld(); puzzleIntro puzzleIntro = (puzzleIntro) getWorld(); if (world != null) world.buttonCommand(name); //Sending the world CLICKED INSTRUCTIONS if (puzzleIntro != null) puzzleIntro.buttonCommand(name); }
public void buttonCommand(String command) { if (command == "Start") { Greenfoot.setWorld(new puzzleIntro()); } }