微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐