cocos2d-x制作弹出层(Layer)

前端之家收集整理的这篇文章主要介绍了cocos2d-x制作弹出层(Layer)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//1.点击后直接在当前页面添加Layer

auto layer = SetLayer::create();
this->addChild(layer);

2.设置弹窗界面SetLayer.h

#ifndef __SET_LAYER_H__
#define __SET_LAYER_H__

#include "cocos2d.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
using namespace cocos2d::ui;

class SetLayer : public cocos2d::Layer
{
public:
    virtual bool init();
    CREATE_FUNC(SetLayer);

private:
    ImageView* Image_back;
    Layout* Panel_1;
    void closeLayerTouch(cocos2d::Ref *pSender,Widget::TouchEventType type);
    void close(Node* pSender);

};

#endif // __SET_LAYER_H__

设置弹窗界面SetLayer.cpp

#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "SetLayer.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace cocos2d::ui;

bool SetLayer::init()
{
    //////////////////////////////
    // 1. super init first
    if (!Layer::init())
    {
        return false;
    }

    auto rootNode = CSLoader::createNode("SetLayer.csb");
    Panel_1 = (cocos2d::ui::Layout*)rootNode->getChildByName("Panel_1");
    auto director = Director::getInstance();
    //1.真机可视区域
    Rect vidiableRect = director->getOpenGLView()->getVisibleRect();
    Panel_1->setAnchorPoint(Vec2(0.5,0.5));
    //居中
    Panel_1->setPosition(Vec2(vidiableRect.origin.x+ vidiableRect.size.width/2,vidiableRect.origin.y+ vidiableRect.size.height/2));

    //2.关闭按钮
    Image_back = (ImageView*)Helper::seekWidgetByName(Panel_1,"Image_back");
    Image_back->setTouchEnabled(true);
    Image_back->addTouchEventListener(CC_CALLBACK_2(SetLayer::closeLayerTouch,this));
    Image_back->setTag(1);

    addChild(rootNode);

    //缩放至可见
    Panel_1->setScale(0);
    Panel_1->runAction(ScaleTo::create(0.2,1.0));
    return true;
}

void SetLayer::closeLayerTouch(cocos2d::Ref * pSender,Widget::TouchEventType type)
{
    switch (type)
    {
    case cocos2d::ui::Widget::TouchEventType::ENDED:
        //手指点中按钮,松开。
        CCLOG("ENDED");
        if (((ImageView*)pSender)->getTag() == 1) {
            //关闭当前层(先缩放至不可见,再执行函数移除当前layer)
            auto seq = Sequence::create(ScaleTo::create(0.2,0),CallFuncN::create(CC_CALLBACK_1(SetLayer::close,this)),NULL);
            Panel_1->runAction(seq);
        }
        break;
    }
}

void SetLayer::close(Node * pSender)
{
    this->removeFromParentAndCleanup(true);
}
原文链接:https://www.f2er.com/cocos2dx/340516.html

猜你在找的Cocos2d-x相关文章