Cocos2d-x 3.x游戏开发之旅读书笔记(3)

来源:互联网 发布:php 响应时间设置 编辑:程序博客网 时间:2024/05/18 01:49

一、schedule

1scheduleUpdateupdate

1this->scheduleUpdate();

2virtual voidupdate(float dt);//重写update函数

3update函数的具体实现

2、使用schedule调用自定义函数

1)自定义函数

2)实现函数

3this->schedule(schedule_selector(自定义的函数),float型的时间)

3、停止函数更新

1)与函数更新对应,“this->scheduleUpdate()”对应“this->unscheduleUpdate()”,“this->schedule(schedule_selector(自定义的函数),float型的时间)”对应“this->unschedule(schedule_selector(自定义的函数))

注意:取消时只需要传递函数,不需要传递其他参数

2this->unscheduleAllSelectors();//停止所以函数更新

4、函数更新一次scheduleOnce

1)自定义函数

2)实现函数

3this->scheduleOnce(schedule_selector(自定义的函数),float型的时间)

5、要获得准确的时间要利用update或自定义函数的float参数进行累计

二、观察者模式工具类--NotificationCenter

1、在两个Layer中进行通信

1)第一个Layer中:

init函数中加入:

this->schedule(schedule_selector(MyHelloWorldScene::sendMsg),3.0f);//三秒后发送消息

函数实现:

voidMyHelloWorldScene::sendMsg(float dt)

{

NotificationCenter::getInstance()->postNotification("test",NULL);//发布test信息,不传递数据

}

2)第二个Layer中:

init函数中加入:

NotificationCenter::getInstance()->addObserver(

this,

callfuncO_selector(SecondScene::testMsg),

"test",

NULL);//不传递数据

函数实现:

void SecondScene::testMsg(Ref* data)//传递数据时data接收到数据

{

log("SecondScene");

}

注意:addObserverpostNotification不能同时传递数据(除非数据相同)

 

三、内存管理机制

Cocos2dxcreate函数都会使用autorelease函数,若对象没有被持有就会被释放,类似C++的智能指针。对这方面比较了解就不详细记录了。

retainrelease:持有与释放(只有在调用autorelease函数后才能生效)

 

四、保存机制

UserDefault::getInstance()->setStringForKey("myName","wuguangyuan");//保存数据,参数分别为keyvalue

//获取数据,key和获取失败返回的数据

std::stringmyName = UserDefault::getInstance()->getStringForKey("myName","none");

log("myname is %s", myName.c_str());

运行一次后已经保存了myName的数据,注释掉setStringForKey后运行依然可以获得数据

类似的还有bool型,integer型,float型和double

 

五、有限状态机

这部分内容比较绕,下面是我理顺的思路:

一个对象有多种状态,每种状态都有各自的行为,行为结束时会转换状态,然后根据转换后的状态实现行为,这样不断地在各种状态直接转换,执行各种行为。

具体就不介绍了,一来没办法解释清楚,二来在网络上有很多相关的更加具体的内容,如果忘记了可以直接查找其他比较详细的博客。

 

0 0