Handling touches in HudLayer and GameLayer

I am using cocos2dx v3.4.
I created a scene which has two layers.

MainScene

 -GameLayer

 -HudLayer

The GameLayer has all the main objects of the game and the HudLayer has pause button,score label etc.

When i update the gamelayer and the layer scrolls , the nodes of HudLayer are also moving which should not happen.

I am posting the code of how i created the scene with two layers.

Scene *MainScene::createScene()
{
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask( PhysicsWorld::DEBUGDRAW_ALL );
scene->getPhysicsWorld()->setGravity(Vect(0,-10));
scene->getPhysicsWorld()->setAutoStep(false);

GameLayer *layer1 = GameLayer::create();
HUDLayer *layer2 = HUDLayer::create();

scene->addChild(layer1,1);
scene->addChild(layer2,2);

return scene;

}

Can you please tell me how to make the HudLayer static so that it is always on top of the screen while the GameLayer scrolls?

make sure you are only scrolling your GameLayer not the whole scene, and it should work

Only the GameLayer and the nodes of GameLayer are made to scroll. The update method is also in the GameLayer.

The HudLayer consists of only a pause button sprite at the top right corner and no other methods.

i am Using 3.3 as of now and this is my setup

     Scene* HelloWorld::createScene() {
      	auto scene = Scene::create();
     	auto layer = HelloWorld::create();
        auto hud = HudLayer::create();
        scene->addChild(layer);
        layer->hudLayer = hud;
        scene->addChild(hud, 20);

	return scene;

}

and in my update (HelloWorld) i have.

             void HelloWorld::update(float dt){
             ...... // some calculations
             this->setPosition(ScrollSpeed);
             }

and this works fine for me, make sure your GmeLayer and Hud Layer extends from Node/Layer

Why is layer->hudLayer = hud used here?

Yeah, GameLayer and HudLayer extends from Layer and MainScene extends from Scene

i like to keep reference of HudLayer for ease of access, as i like to keep my controls(joystick etc) on HudLayer i can directly access them using HudLayer refence.
This Doesn’t make any sense to me this has worked for me all these time, mean time you can workaround this issue like

        void HelloWorld::update(float dt){
         ...... // some calculations
         Vec2 ScrollSpeed; // some predefined scroll ratio
         Vec2 newPos = Vec2(hud->getposition() - ScrollSpeed);
         
         hud->setPosition(newPos);
        // making HelloWorldLayer move.
         this->setPosition(ScrollSpeed):
         }

Thanks for your help.

The problem was with the Camera position. As the layer scrolls, the camera’s position too changes making the HudLayer to scroll as well. Solved it and it’s working now!

And now the Touches on GameLayer works but touches on HudLayer doesnt.

The pause button is a MenuItemImage. On clikcing the pause button nothing happens.

can you post your camera code, if you are not doing something fancy with camera then you could try my approach, its simple and it always works

Does Camera cause problems for TouchEvents?

In the init() method–>

Camera *cam = Camera::create();
cameraAction = new ActionCamera();
this->addChild(cam);
cameraAction->startWithTarget(cam);

In the update method–>

void GameLayer::update(float dt){

   cameraAction->setEye(-(copCam.x),-(copCam.y), z);

   cameraAction->setCenter(Vec3(-(copCam.x),-(copCam.y),0));

}

copCam is Vec2 position.