zoukankan      html  css  js  c++  java
  • cocos2d-x-3.1 NotificationCenter (coco2d-x 学习笔记八)

    在这里分享记录自己的学习NotificationCenter时候的代码,这里用NotificationManager进行管理使用NotificationCenter。


    NotificationManager.cpp

    #include "NotificationManager.h"
    #include "VisibleRect.h"
    
    #define MSG_SWITCH_STATE "SwitchState"
    
    USING_NS_CC;
    
    enum
    {
    	kSpriteTag
    };
    
    
    void runManager(){
    
    	auto mSene = Scene::create();
    
    	auto mLayer = NotificationManager::create();
    
    	mSene->addChild(mLayer);
    
    	Director::getInstance()->runWithScene(mSene);
    
    }
    
    bool NotificationManager::init(){
    
    	if (!Layer::init())
    	{
    		return false;
    	}
    
    	auto sp = Sprite::create("Images/t1.png");
    	sp->setPosition(VisibleRect::center());
    	addChild(sp, 1, kSpriteTag);
    
    	//首先创建一个关闭键
    	auto mCloseItem = MenuItemFont::create("Close", CC_CALLBACK_1(NotificationManager::closeManager, this));
    	//设置关闭键相对坐标
    	mCloseItem->setPosition(VisibleRect::rightBottom().x - mCloseItem->getContentSize().width, VisibleRect::rightBottom().y + mCloseItem->getContentSize().height);
    	auto mCloseMenu = Menu::create(mCloseItem, 0);
    	//设置Menu从坐标系的(0,0)处開始
    	mCloseMenu->setPosition(Vec2::ZERO);
    	addChild(mCloseMenu);
    
    	/*创建一个可开关的Menu*/
    	auto label1 = LabelTTF::create("switch off", "fonts/Marker Felt.ttf", 26);
    	auto label2 = LabelTTF::create("switch on", "fonts/Marker Felt.ttf", 26);
    	auto mSwitchItem1 = MenuItemLabel::create(label1);
    	auto mSwitchItem2 = MenuItemLabel::create(label2);
    	auto mSwitchToggle = MenuItemToggle::createWithCallback(CC_CALLBACK_1(NotificationManager::switchToggle, this), mSwitchItem1, mSwitchItem2, NULL);
    	mSwitchToggle->setSelectedIndex(1);  //switch on选中显示
    	auto mSwitchMenu = Menu::create(mSwitchToggle, NULL);
    	mSwitchMenu->setPosition(Vec2(VisibleRect::bottom().x, VisibleRect::bottom().y + 100));
    	addChild(mSwitchMenu);
    
    	setIsConnectToggle(true);  //进行监听
    
    	return true;
    }
    
    
    void NotificationManager::switchToggle(Ref* sender){
    	auto obj = (MenuItemToggle*)sender;
    
    
    	auto sp1 = (Sprite*)getChildByTag(kSpriteTag);
    
    	if (obj->getSelectedIndex()){  //switch on
    		sp1->setOpacity(255);      //透明度100%
    	}
    	else{                         //switch off
    		sp1->setOpacity(127);    //半透明
    	}
    
    
    }
    
    void NotificationManager::setIsConnectToggle(bool b){
    	if (b){
    		NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(NotificationManager::switchToggle), MSG_SWITCH_STATE,NULL);
    	}
    	else{
    		NotificationCenter::getInstance()->removeObserver(this, MSG_SWITCH_STATE);
    	}
    }
    
    
    NotificationManager::~NotificationManager(){
    	NotificationCenter::getInstance()->removeObserver(this, MSG_SWITCH_STATE);
    }
    
    //退出
    void NotificationManager::closeManager(Ref *sender){
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
    	return;
    #endif
    
    	Director::getInstance()->end();
    
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    	exit(0);
    #endif
    }

    NotificationManager.h

    #ifndef NOTIFICATIONMANAGER_H
    #define NOTIFICATIONMANAGER_H
    #include "cocos2d.h"
    
    class NotificationManager :public cocos2d::Layer
    {
    public:
    	virtual ~NotificationManager();
    
    	virtual bool init();
    
    	void closeManager(cocos2d::Ref *sender);
    
    	void switchToggle(cocos2d::Ref *sender);
    
    	void setIsConnectToggle(bool b);
    
    	CREATE_FUNC(NotificationManager);  //创建一个自己主动释放对象
    };
    
    void runManager();
    
    #endif /*NOTIFICATIONMANAGER_H*/



  • 相关阅读:
    【洛谷】P1156 垃圾陷阱【DP】
    【洛谷】P1063 能量项链【区间DP】
    Android
    Android
    Android中使用ViewPager制作广告栏效果
    Android UI 设计之 TextView EditText 组件属性方法最详细解析
    GitHub 优秀的 Android 开源项目
    下载 编译 Android源代码 和 Android kernel源代码
    UML建模语言入门 -- 静态图详解 类图 对象图 包图 静态图建模实战
    Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7099316.html
Copyright © 2011-2022 走看看