In 3.16, How you add a UI Layer to a Scene?


#1

Hi, i´ve update to 3.16, and i cant add a UI Layer to a scene, i used to do this:

    Scene* HelloworldScene::createScene()
    {
     // 'scene' is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = HelloworldScene::create();       
scene ->addChild(layer);
   
    auto UILayer = Ui::create();	
    scene ->addChild(UILayer ,1);

// return the scene	
return scene;
    }

But now the HelloworldScene is a Scene, not a Layer, i can add a layer, but now its a child of HelloworldScene, and its moves with it, UI doesn´t supposed to do that.

How can i do that now?, thanks

pd: i can change HelloworldScene class to public cocos2d::Layer, and its works, but probably there is a reason for be a Scene now.


#2

Is there any errors you can provide?


#3

there isnt errors, just i dont know where to add the UI layer, when i do this:

     Scene* HelloWorldScene::createScene()
      {
    	auto Scene = HelloWorldScene::create();

    	auto UILayer = Ui::create();        	
    	Scene->addChild(UILayer ,1);
    	
    	return Scene;
    }

the UI layer moves with the HelloWorldScene


#4

How do you know it doesn’t work? There isn’t any content size defined nor anything visible like a Sprite on the UILayer.

Also, shy away from using class names as variable names. It is bad and makes code much harder to read.


#5

What im trying to do is:

I´ve doing this:

class HelloWorldScene : public cocos2d::Layer

Scene* HelloWorldScene ::createScene()
{
	auto scene = Scene::create();

	auto playingLayer = HelloWorldScene ::create();	
	scene->addChild(playingLayer);

	auto uiLayer = Ui::create();	
	scene->addChild(uiLayer, 1);

	return scene;
}

But Now, Since HelloWorldScene is Scene, not Layer :

class HelloWorldScene : public cocos2d::Scene

Scene* HelloWorldScene ::createScene()
{
	auto scene = HelloWorldScene ::create();
		
	auto uiLayer = Ui::create();
	scene->addChild(uiLayer, 1);

	return scene;
}

The Buttons are in the UiLayer, and i moving the HelloWorldScene this way:
this->setPositionY(this->getPositionY() + 5);


#6

Just use Layer instead of UILayer


#7

Ui class is Layer
class Ui : public cocos2d::Layer

For now, i created a node where i add everything except the Ui, and when i want to move the camera, i just move that node

Thanks for your time.


#8

Yes, but UILayer is a bit different. For adding Buttons, ScrollView, ListView, Slider, etc, etc

Layer is your answer. Node is fine too, but you still must add the Node to the Layer.