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

2016/2/29

Help me for switching world

thewisegamer thewisegamer

2016/2/29

#
Hi I'm making a game that have 2 levels. When my actor get a flag in level 1, it brings him to level 2. It success and bring him to level 2, but when I place coin and want go get it, then a window pop up and says: java.lang.ClassCastException: world2 cannot be cast to world at orang.exit(orang.java:35) at orang.act(orang.java:28) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) ( I have actor "coin" and want to be use in level 1 and 2.. Can someone help me? Thanks
danpost danpost

2016/2/29

#
Please show the code of your 'exit' method of the orang class.
thewisegamer thewisegamer

2016/2/29

#
here:
public void exit()
{
        if (getcoin(coin.class))
        ((world)getWorld()).nextLevel();
    }
public boolean getcoin(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
public void get(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }   
thewisegamer thewisegamer

2016/2/29

#
Im sorry Im wrong about my question, its not when the actor get the flag, but when the actor get the coin, it brings him to level 2. sorry for make you confused
danpost danpost

2016/2/29

#
thewisegamer wrote...
Im sorry Im wrong about my question, its not when the actor get the flag, but when the actor get the coin, it brings him to level 2. sorry for make you confused
Fortunately, you copied and posted the error trace. That is how I knew what code you needed to post. Line 4 of your code post tried to cast the world the coin is in to a 'world' type world where the coin could be in a different type of world -- a 'world2' type world (I am guessing at the name of your level 2 world). So, you need to determine which type world the coin is in before you can cast the return of 'getWorld' to a specific type:
if (getWorld() instanceof world) ((world)getWorld()).nextLevel();
else if (getWorld() instanceof worldd2) ((world2)getWorld()).nextLevel();
thewisegamer thewisegamer

2016/2/29

#
yes world2 means my level 2. what is the meaning of "instanceof"?
danpost danpost

2016/2/29

#
thewisegamer wrote...
what is the meaning of "instanceof"?
Anytime you create an object (or instantiate a class), you are creating an instance of that class. The 'instanceof' keyword is used to determine if an object belongs to a specific class. It returns a boolean (true/false) value indicating whether the object belongs to the class given (your initial world class is instantiated when you reset the project).
davmac davmac

2016/2/29

#
danpost wrote...
Anytime you create an object (or instantiate a class), you are creating an instance of that class. The 'instanceof' keyword is used to determine if an object belongs to a specific class
This is correct, but note that you should not read this as saying that an object can be an "instance of" only one class. If the "MyWorld" class extends "World", then an instance of MyWorld is also an instance of World. Also, "instanceof" can be used to test for whether an object implements an interface, though this is a slightly more advanced topic and is not a normal use in Greenfoot.
thewisegamer thewisegamer

2016/3/1

#
okay i will try, thankyou davmac and danpost :)
fearDnine fearDnine

2016/3/1

#
where do you come from? @thewisegamer
thewisegamer thewisegamer

2016/3/1

#
@fearDnine Indonesia
You need to login to post a reply.