前言
整理代码的时候发现一个问题,创建Scene或者替换Scene后,在同一帧内获取不到runningScene,至少延后一帧后才能获取到,抱着疑问找了一下问题,记录一下。
源码
- 获取running scene
cc.Director:getInstance():getRunningScene()
/** Gets current running Scene. Director can only run one Scene at a time. */
Scene* getRunningScene() { return _runningScene; }
通过Director可以获取到正在运行的scene,c++代码可以看到返回的是_runingScene
- _runingScene赋值
void Director::setNextScene()
{
......
_runningScene = _nextScene;
......
}
// Draw the Scene
void Director::drawScene()
{
......
/* to avoid flickr, nextScene MUST be here: after tick and before draw.
* FIXME: Which bug is this one. It seems that it can't be reproduced with v0.9
*/
if (_nextScene)
{
setNextScene();
}
......
}
通过查代码可以看到,_runningScene是在setNextScene里面被赋值,setNextScene则是在drawScene里被调用,基本这里就可以看出问题了,因为drawScene并不是在runScene或者replaceScene里直接调用的,是在mainloop里被调用的,所以就慢了一帧
- 问题
/* to avoid flickr, nextScene MUST be here: after tick and before draw.
* FIXME: Which bug is this one. It seems that it can't be reproduced with v0.9
*/
正当我疑惑之际,看到了遗留的注释,好吧,不乱动了,就先这样吧
推送
- Github
https://github.com/KingSun5
结语
若是觉得博主的文章写的不错,不妨关注一下博主,点赞一下博文,另博主能力有限,若文中有出现什么错误的地方,欢迎各位评论指摘。
QQ交流群:806091680(Chinar)
该群为CSDN博主Chinar所创,推荐一下!我也在群里!
本文属于原创文章,转载请著名作者出处并置顶!!