头文件Popup.h
#ifndef _POPUP_H_#define _POPUP_H_#include "cocos2d.h"#include "GmbsCocos.h"#include "ui/CocosGUI.h"USING_NS_CC;class Popup : public Layercolor{ //typedef std::function<voID(voID)> showEnd_t;public: Popup(); ~Popup(); bool create(Size &size,const char* bgfilename,bool autorelease = true); virtual bool init(); voID show(float seconds,bool removeDWhenShowEnd = true,std::function<voID(voID)> showEndCallback = nullptr); voID show01(float seconds,std::function<voID(voID)> showEndCallback = nullptr); voID showEnd(); voID showWithAction(ActionInterval* action,bool removeDWhenShowEnd,std::function<voID(voID)> showEndCallback); voID setVisible(bool visible); voID setHIDeCallback(std::function<voID(voID)> showEndCallback) { m_showEndCallback = showEndCallback; } voID setHIDeWhentouch(bool hIDeWhentouch) { m_hIDeWhentouch = hIDeWhentouch; }protected: virtual bool ontouchBegan(touch *ptouch,Event *pEvent); EventListenertouchOneByOne* m_touchListener; ui::Scale9Sprite* m_background; std::function<voID(voID)> m_showEndCallback; bool m_removeDWhenShowEnd; bool m_hIDeWhentouch;};#endif
源文件Popup.cpp
#include "Popup.h"Popup::Popup(){ m_touchListener = nullptr; m_showEndCallback = nullptr; m_hIDeWhentouch = false; m_removeDWhenShowEnd = false;}Popup::~Popup(){}bool Popup::create(Size &size,bool autorelease){ if (!this->initWithcolor(color4B(0,0,0))) return false; if (autorelease) this->autorelease(); this->setContentSize(size); m_background = nullptr; if (bgfilename != nullptr) { GmbsPoint pt; m_background = ui::Scale9Sprite::create(bgfilename); this->addChild(m_background); m_background->setAnchorPoint(Vec2(0.5,0.5)); m_background->setContentSize(size); m_background->setcolor(color3B(255,133,52)); m_background->setopacity(220); pt.reset(m_background); pt.xMIDdleAlign(this).yMIDdleAlign(this); m_background->setposition(pt); Size frameSize(size.wIDth + 4,size.height + 4); ui::Scale9Sprite* frame = ui::Scale9Sprite::create(bgfilename); this->addChild(frame,-1); frame->setAnchorPoint(Vec2(0.5,0.5)); frame->setContentSize(frameSize); frame->setcolor(color3B(255,255,255)); pt.reset(frame); pt.xMIDdleAlign(m_background).yMIDdleAlign(m_background); frame->setposition(pt); } setVisible(true); return this->init();}bool Popup::init(){ return true;}voID Popup::setVisible(bool visible){ if (visible && !m_touchListener) { m_touchListener = EventListenertouchOneByOne::create(); m_touchListener->ontouchBegan = CC_CALLBACK_2(Popup::ontouchBegan,this);; m_touchListener->setSwallowtouches(true); _eventdispatcher->addEventListenerWithSceneGraPHPriority(m_touchListener,this); } else if (!visible && m_touchListener) { _eventdispatcher->removeEventListener(m_touchListener); m_touchListener = nullptr; } Layercolor::setVisible(visible);}bool Popup::ontouchBegan(touch *ptouch,Event *pEvent){ if (m_hIDeWhentouch) { Size size = getContentSize(); Rect boundingRect(0,size.wIDth,size.height); Point touchPoint = converttouchToNodeSpace(ptouch); if (!boundingRect.containsPoint(touchPoint)) { showEnd(); } } return true;}voID Popup::show(float seconds,std::function<voID(voID)> showEndCallback){ auto action = Sequence::create( DelayTime::create(seconds),CallFunc::create(CC_CALLBACK_0(Popup::showEnd,this)),nullptr); this->showWithAction(action,removeDWhenShowEnd,showEndCallback);}voID Popup::show01(float seconds,std::function<voID(voID)> showEndCallback){ setScale(0); auto action = Sequence::create( Scaleto::create(0.8f,1.25f),DelayTime::create(seconds),showEndCallback);}voID Popup::showWithAction(ActionInterval* action,std::function<voID(voID)> showEndCallback){ this->stopActionByTag(1); action->setTag(1); this->runAction(action); setVisible(true); m_removeDWhenShowEnd = removeDWhenShowEnd; m_showEndCallback = showEndCallback;}voID Popup::showEnd(){ setVisible(false); if (m_showEndCallback) m_showEndCallback(); if (m_removeDWhenShowEnd) this->removeFromParent();}总结
以上是内存溢出为你收集整理的cocos2dx源码:popup实现全部内容,希望文章能够帮你解决cocos2dx源码:popup实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)