Hi! I have a world which fills with buttons that show which worlds are available, so the player can click on one and start the world that's connected to that button. That works fine except for one tiny thing and I can't figure out how to manage that.
When a button in this world is clicked, the button-object starts the method 'startThisWorld' (lines 34-41) which also gets a number (roomID) sent to it. I want that number to be the choice that's made to say which world will be next. First I tried a switch-case-statement:
The compiler complained in the last line of startThisWorld() that my world-variable might not be initialized.
Then I tried if-statements:
The same happened.
Only when I write something like:
it works. But that's not what I want. What can I do?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.HashMap;
import java.util.Map;
import java.awt.Color;
public class Rooms extends World
{
public int roomID;
public String anzeige;
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
Spielwelt copyworld;
Map<Integer, Room> rm = new HashMap<Integer, Room>();
Levelquest level;
Backyard backyard;
Salon salon;
Park park;
public Rooms(Spielwelt world)
{
super(600, 400, 1);
copyworld = world;
rm = world.getRM().getRoomMap();
this.level = world.getLevelquest();
showRooms();
}
public Map<Integer, Room> getRM()
{
return rm;
}
public void startThisWorld(int roomID)
{
setWorlds(roomID);
Spielwelt world;
switch(roomID)
{
case 1: world = salon; break;
case 2: world = backyard; break;
case 3: world = park; break;
}
Greenfoot.setWorld(world);
}
public void setWorlds(int roomID)
{
Room room = rm.get(roomID);
backyard = new Backyard(level, room);
salon = new Salon(level, room);
park = new Park(level, room);
}
public void fillRoomList(Room room)
{
int listlength = rm.size()+1;
rm.put(listlength, room);
}
public void showRooms()
{
int x = 150;
for (Room r : rm.values())
{
addObject(r, x, 110);
x = x+130;
updateImage(r);
}
}
public void updateImage(Room r)
{
background = r.getImage();
anzeige = r.getAnzeigeName();
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("" + anzeige, 22, Color.BLACK, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
r.setImage(image);
}
}
public void startThisWorld(int roomID)
{
setWorlds(roomID);
Spielwelt world;
if(roomID == 1)
{
world = salon;
}
if(roomID == 2)
{
world = backyard;
}
if(roomID == 3)
{
world = park;
}
Greenfoot.setWorld(world); public void startThisWorld(int roomID)
{
setWorlds(roomID);
Spielwelt world = backyard;
Greenfoot.setWorld(world);
}
