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

2013/2/13

String Issue :(

Draymothisk Draymothisk

2013/2/13

#
I need my main character to know what world he is in, so he receives a String from the world. I was hoping that this String (which its value is the same as the world's name) would be able to act as the class name needed to run a method from that world, but it compiles an error saying "cannot find symbol - class currentWorld". Here is the code (loosely) that I have written. String currentWorld; //Tells what world Guy is currently in public Guy (String checkWorld) //String received from the world in specific, specifying the world's name currentWorld = checkWorld; if(Greenfoot.isKeyDown("enter") & atDoor == true) { levelEnd = true; ((currentWorld)getWorld()).changeWorld(levelEnd); } Any idea why this wont work? If I replace: ((currentWorld)getWorld()).changeWorld(levelEnd); with: ((worldOne)getWorld()).changeWorld(levelEnd); it works fine, but currentWorld's value is equal to "worldOne", so why won't it work?
danpost danpost

2013/2/13

#
You will have to check each type world individually and cast 'currentWorld' to whichever it is determined to be.
World world = getWorld();
if (world instanceof worldOne) worldOne currentWorld = (worldOne)world;
if (world instanceof worldTwo) worldTwo currentWorld = (worldTwo)world;
// etc.  Then you can use
currentWorld.changeWorld(levelEnd);
davmac davmac

2013/2/13

#
danpost, that won't work. You can't have a declaration as the body of an "if" statement and even if you could, the variable would go out of scope immediately after the "if" statement. You could handle each type separately:
if (world instanceof worldOne) {
    worldOne currentWorld = (worldOne)world;
    currentWorld.changeWorld(levelEnd);
}
else if (world instanceof worldTwo) {
    worldTwo currentWorld = (worldTwo)world;
    currentWorld.changeWorld(levelEnd);
}
But, a much better way is to have the worlds implement a common interface:
// In WorldOne:
public class WorldOne implements LevelWorld { ...

// In WorldTwo:
public class WorldTwo implements LevelWorld { ...

// The LevelWorld interface:
public interface LevelWorld
{
    void changeWorld(boolean levelEnd);
}
Then you cast the world to LevelWorld:
 ((LevelWorld)getWorld()).changeWorld(levelEnd); 
Draymothisk:
Any idea why this wont work? If I replace: ((currentWorld)getWorld()).changeWorld(levelEnd); with: ((worldOne)getWorld()).changeWorld(levelEnd); it works fine, but currentWorld's value is equal to "worldOne", so why won't it work?
It won't work because worldOne is a class name, not a a String. "worldOne" is a String, not a class name. When you cast to a type you must use a class name.
danpost danpost

2013/2/13

#
You are so right, davmac. (what I meant was something like your first suggestion above). Glad you chimed in! Thanks.
Draymothisk Draymothisk

2013/2/13

#
For that second example you used Davmac, can you explain that a bit more?
Draymothisk Draymothisk

2013/2/13

#
Also, I got it to work :) Thanks both of you. Just had to change "world" to "getWorld()" off of your code. if(getWorld() instanceof worldOne) { worldOne currentWorld = (worldOne)getWorld(); currentWorld.populateHpBar(guyHP); } else if(getWorld() instanceof worldTwo) { worldTwo currentWorld = (worldTwo)getWorld(); currentWorld.populateHpBar(guyHP); }
danpost danpost

2013/2/13

#
Draymothisk wrote...
For that second example you used Davmac, can you explain that a bit more?
Please refer to the Java tutorial pages on interfaces.
You need to login to post a reply.