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

2012/5/8

"world cannot be cast to world2" Why?

KevDog32 KevDog32

2012/5/8

#
There is "world" and "puzzleIntro" under "World" in my project. In "world" it defines an actor, and then creates a "button" with a parameter.
button button1 = new button("Start");
public world()
    {    
        super(870, 500, 1);         
        addObject (button1, 435, 350);
    }
In "button" it assigns the parameter to the givenName.
private String name;
public button(String givenName)
    {
        name = givenName;
        setImage(name + ".png"); //Setting the right image
    }
When clicked, it the "buttonCommand" method present in "world", send the button's "name"
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);
    }
BACK in "world", it receives the string "Start" for use in the buttonCommand
public void buttonCommand(String command)
    {
        if (command == "Start")
        {
            Greenfoot.setWorld(new puzzleIntro());
        }
    }
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?
danpost danpost

2012/5/8

#
If you are wanting the button to work in BOTH worlds, change your sendCommand() method to the following: (btw, no need to ask if myWorld is null as button cannot be clicked on unless in a world)
public void sendCommand()
{
    World myWorld = getWorld();
    if (myWorld instanceOf world) ((world) myWorld).buttonCommand(name);
    if (myWorld instanceOf puzzleIntro) ((puzzleIntro) myWorld).buttonCommand(name);
}
Also, in your button's 'checkClicked()' method, you do not need 'Greenfoot.getMouseInfo();'. In fact, you are telling Greenfoot to get the info, but where is it going (not anywhere where you can access the info). You would only need this command if you needed specific information about the click, like exactly where the click occurred, or which button was used). Then you would use 'MouseInfo mi = Greenfoot.getMouseInfo();', and use 'mi.getX()' or 'mi.getButton()'. Saving the info to a MouseInfo object allows us access to the specifics of the last mouse action. In your case here, though, you obviously do not need the extra info.
plcs plcs

2012/5/8

#
public void sendCommand()
{
    World myWorld = getWorld();
    if (myWorld instanceOf world) ((world) myWorld).buttonCommand(name);
    if (myWorld instanceOf puzzleIntro) ((puzzleIntro) myWorld).buttonCommand(name);
}
A better idea than writing it like this would be to have an abstract world class that before "world" and "puzzleIntro" extended, which contained an abstract method buttonCommand. Then you could create more subclasses of it, and for the code would become.
public void sendCommand()
{
    ((MyAbstractWorld) myWorld).buttonCommand(name);
}
Inheritance is useful! :)
KevDog32 KevDog32

2012/5/11

#
If they inherit from a parent class, does setWorld still work?
KevDog32 KevDog32

2012/5/11

#
Also, when I use danpost's method, I can't compile as a close parenthesis is expected after "myWorld" in
if (myWorld instanceOf world) ((world) myWorld).buttonCommand(name);
KevDog32 KevDog32

2012/5/11

#
Okay thanks for both your help! I have implemented plcs' method and created a MyAbstractWorld. It works like a charm!
danpost danpost

2012/5/11

#
KevDog32 wrote...
Also, when I use danpost's method, I can't compile as a close parenthesis is expected after "myWorld" in
if (myWorld instanceOf world) ((world) myWorld).buttonCommand(name);
I recall having that problem before. I am not sure if that would be a Greenfoot bug or I am coding it wrong. But this works:
if (getWorld() instanceOf world) ((world) getWorld()).buttonCommand(name);
The only difference being
World myWorld = getWorld();
was used prior to the quoted line.
danpost danpost

2012/5/11

#
Seems to me, it would probably be a Java bug, if it is a bug at all. Though, Greenfoot is the one throwing the error and saying that a close parenthesis is needed.
You need to login to post a reply.