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

2016/5/16

Having a problem with changing the world.

unpeaceful unpeaceful

2016/5/16

#
I am currently trying to change the world with the use of arrow keys, and I've gotten everything working except the actual changing the world part.
                        World changeCurrently = modeStrings[i];
                        Greenfoot.setWorld(changeCurrently);
                        i = secMax;
That is the part that I am currently having the problem with. the "World changeCurrently = modeStrings;", and it says that modeStrings cannot be converted to greenfoot.World There is probably a really obvious solution but everything I could think of doesn't seem to turn out right.
unpeaceful unpeaceful

2016/5/16

#
*bump*, I have no idea if this reply does anything.
SPower SPower

2016/5/16

#
You'll have to show what modeStrings is. What is it an array of?
unpeaceful unpeaceful

2016/5/16

#
SPower wrote...
You'll have to show what modeStrings is. What is it an array of?
It is an array of strings, is there any way to convert strings to a world or even the other way around?
danpost danpost

2016/5/16

#
unpeaceful wrote...
It is an array of strings, is there any way to convert strings to a world or even the other way around?
You would have to do that systematically, one check at a time. Java now supports strings in 'switch' statements. You could possibly do something like this:
World changeCurrently = null;
switch (modeString[i])
{
    case "World1":  changeCurrently = new World1(); break;
    case "World2":  changeCurrently = new World2(); break;
    // etc.
}
if (changeCurrently != null)
{
    // System.out.println(changeCurrently.toString());
    Greenfoot.setWorld(changeCurrently);
}
If your java does not yet support strings in 'switch' statements, you will need to use 'if' statements for each case. Line 10 can be uncommented to show what an objet turned to a string would look like.
unpeaceful unpeaceful

2016/5/16

#
danpost wrote...
unpeaceful wrote...
It is an array of strings, is there any way to convert strings to a world or even the other way around?
You would have to do that systematically, one check at a time. Java now supports strings in 'switch' statements. You could possibly do something like this:
-snip-
If your java does not yet support strings in 'switch' statements, you will need to use 'if' statements for each case. Line 10 can be uncommented to show what an objet turned to a string would look like.
Thank you so much! I've been stressing over this for a while now. I really appreciate my man.
You need to login to post a reply.