How to create a Layer
As mentioned previously, all entities are submitted to the engine through a subclass of the U4DEngine::
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::
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::
//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::
//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");