Windows in coco2d

Hi,
I’m trying to implement the “GameOver” method in my game, and I should like add a window on the screen when the player loses the game. Something Like this:

The background is the game lost scene and the GameOver window appears on the screen. Anyone know any example to do this? The window is a new scene? I have no idea how to do this, I should like view an example.

Thanks

I always do this kind of way.
If there is a better way, I also want to know.

#include "ui/CocosGUI.h"

// the layer set so that it can not touch the under layer elements.
auto winSize = Director::getInstance()->getWinSize();
auto cover = ui::Layout::create();
cover->setTouchEnabled(true);
cover->setSwallowTouches(true);
cover->setContentSize(winSize);
cover->setAnchorPoint(Vec2(0, 0));
cover->setPosition(Vec2(0, 0));
cover->setLocalZOrder(5000); // enough big value
cover->setBackGroundColor(Color3B::BLACK);
cover->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
cover->setBackGroundColorOpacity(170);
this->addChild(cover); // "this" is scene

// add images and buttons, etc on the cover
auto title = Sprite::create("gameover.png");
title->setAnchorPoint(0.5, 1.0);
title->setPosition(winSize.width / 2, winSize.height - 100);
cover->addChild(title);

auto text = ui::Text::create(...);
...
cover->addChild(text);

auto button = ui::Button::create(...);
...
cover->addChild(button);


Thanks @bluewind00 . It works!
But the button does not work yet. This is my code for the button:

cocos2d::ui::Button *bPlay = cocos2d::ui::Button::create("buttonPlay.png");
bPlay->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2)); 
bPlay->addTouchEventListener(CC_CALLBACK_1(Game::onTouchPlay, this)); 

cover->addChild(bPlay); 

This is the error output when I push the button:

Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?

What is my error? This button is for play again the game. I should remove the layer child from the scene if occurs this?
Thanks

Please post the related code.

cover add / remove

Cover is not deleted automatically, so you must delete it before playing the game again.

void Game::showCover() {
    auto cover = ui::Layout::create();
    cover->setName("cover");

    cocos2d::ui::Button *bPlay = cocos2d::ui::Button::create("buttonPlay.png");
    ...
    bPlay->addTouchEventListener(CC_CALLBACK_1(Game::onTouchPlay, this)); 
    ...
    cover->addChild(bPlay);
   ...
}

void Game::onTouchPlay(Ref* ref, ui::Widget::TouchEventType type) {
    // remove cover if exists
    auto cover = getChildByName("cover");
    if(cover != nullptr) {
        cover->removeFromParent();
    }
    ...
});

about error

Sorry, I can not predict the cause of this error.
I have never seen this error so far…

Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?

I want to know if the click of the button is the direct cause.
This code, even if you click bPlay, you just output it to the log.
Does it generate an error when clicking the button?

cocos2d::ui::Button *bPlay = cocos2d::ui::Button::create("buttonPlay.png");
bPlay->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
//bPlay->addTouchEventListener(CC_CALLBACK_1(Game::onTouchPlay, this));
bPlay->addClickEventListener([this](Ref*) {
    CCLOG("click bPlay");
});
cover->addChild(bPlay);

If this does not cause an error, there may be something wrong with Game::onTouchPlay.
Could you post the contents of Game :: onTouchPlay?

Your touch event is being invoked couple times.
Code fix:

void Game::onTouchPlay(Ref* ref, ui::Widget::TouchEventType type) {
    // remove cover if exists
    if(type == ui::Widget::TouchEventType::ENDED){    // Can be used with BEGAN too
        auto cover = getChildByName("cover");
        if(cover != nullptr)
            cover->removeFromParent();
         ...
   }
});

ui::Widget::TouchEventType has 4 events:

BEGAN     // When pressed your mouse button
MOVED     // When you have moved your mouse while holding the mouse button (after BEGAN)
ENDED     // When you have released your mouse button
CANCELED  // When you have released your mouse button not in the button Rectangle zone

So your event probably fired atleast twice at the same time. That’s why you are getting error, due to trying to delete the same node twice, while it’s still in the middle of the process.

Thanks for all answers.
The problem in the button is in my code.
@bluewind00 if I put this code:

bPlay->addClickEventListener([this](Ref*) {
    CCLOG("click bPlay");
});

It works perfectly.

@Dinamix in my onTouchPlay method I don’t use “type” parameter, so I would not make this.
The program crashes when it try delete the singleton object and regenerate another one.
In the onTouchPlay method, I call GameManager::getInstance() and this is the method:

GameManager* GameManager::getInstance()
{
    static GameManager *sharedInstance = new GameManager;
    if(Game::getInstance()->resetGameManagerInstance == true)
    {
        delete sharedInstance;
        sharedInstance = new GameManager;
        Game::getInstance()->resetGameManagerInstance = false;
    }
    
    return sharedInstance;
}

When I push the button, I set resetGameManagerInstance in true. So, in the “delete sharedInstance” line, the program crashes with that error:

Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?

but I don’t understand what should I make for fix it.

in the gameOver method, if I replace the scene instead of create a new Layer in the running scene, works perfectly my code.

I solved it using the singleton forever, without delete the object.
Thanks to all.