How to create a Layer

As mentioned previously, all entities are submitted to the engine through a subclass of the U4DEngine::U4DWorld class. Aside from submitting game entities into the rendering engine, you can also render Layers.

A layer is used to add UI Elements into your game. For example, a layer can contain buttons, images, shader objects, etc. Whenever, you create a layer and add it to the current view, the controller will point to your layer. Any user interaction will be detected by the layer and passed down the game model.

A layer is implemented using the U4DEngine::U4DLayer class. The code snippet below shows how to declare the U4DEngine::U4DLayer subclass.

class MainMenuLayer:public U4DEngine::U4DLayer {

private:

public:

    MainMenuLayer(std::string uLayerName); 

    ~MainMenuLayer();

    void init();

};

Notice that the layer class requires a name to be provided. The init() method is where you would add game entities such as buttons, images, etc. The layer can also have a background image. This is accomplished by using the following method:

setBackgroundImage("myImage.png");

The MainMenuLayer object is then added to the U4DEngine::U4DWorld subclass through the use of the U4DEngine::U4DLayerManager as shown below:

//create layer manager
U4DEngine::U4DLayerManager *layerManager=U4DEngine::U4DLayerManager::sharedInstance();

//set this view (U4DWorld subclass) to the layer Manager
layerManager->setWorld(this);

//create Layers
mainMenuLayer=new MainMenuLayer("menuLayer");

mainMenuLayer->init();

layerManager->addLayerToContainer(mainMenuLayer);

//push layer
layerManager->pushLayer("menuLayer");

Note, that you must push the layer in order for it to become active.

Creating a Layer without creating a subclass

This is another example of how to create a U4DEngine::U4DLayer without creating a subclass:

//create layer manager
U4DEngine::U4DLayerManager *layerManager=U4DEngine::U4DLayerManager::sharedInstance();

//set this view (U4DWorld subclass) to the layer Manager
layerManager->setWorld(this);

//create Layers
U4DEngine::U4DLayer* mainMenuLayer=new U4DEngine::U4DLayer("menuLayer");

//Create buttons to add to the layer
U4DEngine::U4DButton *buttonA=new U4DEngine::U4DButton("buttonA",0.4,-0.6,103.0,103.0,"ButtonA.png","ButtonAPressed.png");

//add the buttons to the layer
mainMenuLayer->addChild(buttonA);

layerManager->addLayerToContainer(mainMenuLayer);

//push layer
layerManager->pushLayer("menuLayer");

Last updated