不说上代码
---head
class MyDelegate
{
public:
virtual void onGameStart(void) = 0;
virtual void onGameEnd(void) = 0;
protected:
private:
};
class GameLayer : public cocos2d::Layer
{
public:
static cocos2d::Scene* scene();
virtual bool init();
CREATE_FUNC(GameLayer);
CC_SYNTHESIZE(MyDelegate*, delegator, Delegator);
/*CC_SYNTHESIZE =
protected:
MyDelegate* delegate;
public:
virtual MyDelegate* getDelegator(void) const { return delegate};
virtual void setDelegator(MyDelegate* dele) { delegate = dele};
*/
};
class StatusLayer : public cocos2d::Layer, public MyDelegate
{
public:
virtual bool init();
CREATE_FUNC(StatusLayer);
void onGameStart() override;
void onGameEnd() override;
protected:
private:
};
---cpp
//---------------------------------------------------------------------
bool StatusLayer::init()
{
if ( !Layer::init() )
{
return false;
}
return true;
}
void StatusLayer::onGameStart()
{
CCLOG("GAME START ON STATUS
cocos设计模式委托实践
最新推荐文章于 2019-03-04 16:18:40 发布
本文介绍了在Cocos2d-x游戏开发中如何使用委托设计模式。通过定义一个抽象委托类`MyDelegate`,包含`onGameStart`和`onGameEnd`两个纯虚函数,实现游戏开始和结束的回调。`GameLayer`类中持有`MyDelegate`的指针,并提供了设置和获取委托的方法。`StatusLayer`继承自`Layer`和`MyDelegate`,实现了回调函数。在`GameLayer`的初始化过程中,将`StatusLayer`作为委托,调用其`onGameStart`和`onGameEnd`方法,展示了委托模式在游戏状态管理中的应用。
摘要由CSDN通过智能技术生成