Game Development Reference
In-Depth Information
How to do it...
Perform the following set of steps to load a level:
1. We start by defining a new message class:
LoadLevelMessage
. It extends
GameMessage
since it might be useful to know the
gameId
. Apart from that, it
has one field
levelName
.
2. We'll add the same field to our
Game
class so that it can keep track of which level
it's running.
3. Next, let's create a
levelNode
field on our server, which we can load our level
into, as follows:
private Node loadLevel(String levelName){
return (Node) assetManager.loadModel("Scenes/
"+levelName + ".j3o");
}
4. Then, we create a small method that will load the level from a predefined path, as
follows:
levelNode = loadLevel("TestScene");
rootNode.attachChild(levelNode);
game.setLevelName("TestScene");
5. Inside the
simpleInitApp
method, we'll tell the application to load
TestS-
LoadLevelMessage levelMessage = new LoadLevelMessage();
levelMessage.setLevelName(game.getLevelName());
server.broadcast(Filters.in(conn), levelMessage);
6. Finally, inside the
addPlayer
method, we need to create and send the message
to the connecting client. That's all for the server side of things.
7. In the client, we create a
levelNode
field and a
loadLevel
method, but it's a
little bit different:
public void loadLevel(final String levelName){
enqueue(new Callable(){
public Object call() throws Exception {
if(rootNode.hasChild(levelNode)){