Cocos2dx 3.0rc事件监听和消息通知

Cocos2dx 3.0rc事件监听

1、新建一个存放数据的类

class AAA
{
public:
	CC_SYNTHESIZE(std::string, name, Name);
	CC_SYNTHESIZE(int, age, Age);
	~AAA(){ CCLOG("delete"); }
};

2、新建一个”yang”的监听器,

方式1:利用C++lambda表达式

auto myListener = EventListenerCustom::create("yang", [=](EventCustom* event){
		AAA* b = (AAA*)(event->getUserData());
		CCLOG("getUserDate: name=%s , age=%d", b->getName().c_str(), b->getAge());
delete b;
	});
	//委派这个监听器,_eventDispatcher是Node类上的成员变量,继承后可以直接使用
	_eventDispatcher->addEventListenerWithFixedPriority(myListener, 1);

方式2:回调函数方式

auto myListener = EventListenerCustom::create("yang", CC_CALLBACK_1(HelloWorld::myEventCustom,this));
	//分配这个监听器
	_eventDispatcher->addEventListenerWithFixedPriority(myListener, 1);

void HelloWorld::myEventCustom(EventCustom* event)
{
	AAA* b = (AAA*)(event->getUserData());
	CCLOG("getUserDate: name=%s , age=%d", b->getName().c_str(), b->getAge());
delete b;
}

3、创建事件yang并添加数据,就可以通知yang监听器并获取数据

if (count == 3)
	{
		AAA a;
		a.setName("myname");
		a.setAge(8);

		EventCustom event("yang");
		event.setUserData(&a);
		//分配事件
		_eventDispatcher->dispatchEvent(&event);
	}

这事件的创建我写在定时器里,count每秒+1;当满足条件时就创建这个yang时间

运行结果:
getUserDate: name=myname , age=8
delete

Cocos2dx 3.0rc消息通知

1、在消息中心添加一个要监听的消息xuan,回调函数是ShowGameOver,传递到回调函数的参数是NULL

	//监听一个xuan的消息,回调函数是ShowGameOver,
	NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(HelloWorld::ShowGameOver), "xuan", NULL);
void HelloWorld::ShowGameOver(Ref* pObj)
{
	if (pObj != NULL)
	{
		auto sp = (Sprite*)pObj;
		CCLOG("Game over ! tag=%d", sp->getTag());
	}
}

2、往消息中心发送一个xuan消息,传递参数为sp(参数必须为Ref的子类)

	if (count == 6)
	{
		auto sp = Sprite::create("HelloWorld.png");
		sp->setTag(180);
		//发送一个xuan的消息,参数为sp,第二个参数必须是Ref的子类
		NotificationCenter::getInstance()->postNotification("xuan", sp);
	}

3、运行结果

Game over ! tag=180

4、必须注意的问题

NotificationCenter::getInstance()->addObserver(this,callfuncO_selector(HelloWorld::ShowGameOver),"xuan",NULL);

NotificationCenter::getInstance()->postNotification("xuan",sp);

监听器和发射器的最后一个参数必须有一个为NULL,不然回调函数获取不到传递的参数,两个参数不知获取那个。

哪个不为NULL就获取哪个。

事件监听和消息通知的区别

区别在于传递的参数,事件监听可以传递任意类型的参数,而消息通知必须传递Ref的子类,也就是cocos2dx里面的object类



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Box2D和Cocos2D是两个游戏开发框架。Box2D是一个用于物理模拟的开源库,可以模拟刚体的运动和碰撞等物理效果。Cocos2D是一个用于2D游戏开发的跨平台框架,提供了丰富的图形渲染和用户交互功能。\[1\] 在使用Cocos2D和Box2D进行游戏开发时,你可以利用Box2D来处理游戏中的物理效果,比如重力、碰撞和刚体运动等。Cocos2D提供了与Box2D的集成,使得开发者可以方便地在Cocos2D中使用Box2D的功能。你可以通过创建物理世界、添加刚体和设置碰撞检测等来实现游戏中的物理效果。\[2\] 如果你刚刚接触Cocos2D和Box2D,建议你先学习Cocos2D和Box2D的基础知识,然后再深入学习如何在Cocos2D中使用Box2D。你可以参考一些入门教程,比如《Cocos2D入门》和《Box2D入门》。如果你对OpenGL ES 2.0和自定义Cocos2D 2.X着色器等背景知识感到困惑,你可以查阅相关教程来获取更多的帮助。\[3\] #### 引用[.reference_title] - *1* *3* [如何使用Box2D和Cocos2D制作一款像Fruit Ninja一样的游戏-第1部分](https://blog.csdn.net/kaka626/article/details/9397825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [用Box2D和cocos2d-x制作弹弓类游戏](https://blog.csdn.net/qq55008307/article/details/8090839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝶泳奈何桥.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值