求助:
目前引擎版本是3.3rc2 Lua版本 创建Sprite3D模型,半透明的情况下(setOpactity),在update每帧刷新x,y坐标移动的时候,小米3、三星Note等真机上会非常的卡顿。
所以写了两个版本进行了对比:
Lua实现版本:卡顿时,帧频率直接降到20到30多,发现一个规律:每次运行程序时,这种效率问题必现!但是切入后台,再返回到游戏后,效率又恢复正常了,很诡异啊!
C++实现版本:正常60帧左右
因为是项目初期,这个效率问题很严重,直接影响到我们的3D项目能否继续进行下去,希望官方大牛能够帮忙解决一下~多谢!
请问是使用Cocos Code IDE么?
恩 是的 都是最新版本
将 appDelegate.cpp 文件中的 initRuntime 和 startRuntime 调用去掉,再观察帧率变化
把结果反馈一下,3Q~
我试了下,注释掉 initRuntime 和 startRuntime 以后vs跑不起来了,修改了COCOS2D_DEBUG=0也用不了
直接运行exe,不要通过ide运行,ide在后台做了很多东西,跑起来巨慢
bool AppDelegate::applicationDidFinishLaunching()
{
//#if (COCOS2D_DEBUG > 0)
// NOTE:Please don’t remove this call if you want to debug with Cocos Code IDE
// initRuntime();
//#endif
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
Size viewSize = ConfigParser::getInstance()->getInitViewSize();
string title = ConfigParser::getInstance()->getInitViewName();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
extern void createSimulator(const char* viewName, float width, float height, bool isLandscape = true, float frameZoomFactor = 1.0f);
bool isLanscape = ConfigParser::getInstance()->isLanscape();
createSimulator(title.c_str(),viewSize.width,viewSize.height, isLanscape);
#else
glview = cocos2d::GLViewImpl::createWithRect(title.c_str(), Rect(0, 0, viewSize.width, viewSize.height));
director->setOpenGLView(glview);
#endif
}
auto engine = LuaEngine::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(engine);
lua_State* L = engine->getLuaStack()->getLuaState();
lua_module_register(L);
// If you want to use Quick-Cocos2d-X, please uncomment below code
// register_all_quick_manual(L);
LuaStack* stack = engine->getLuaStack();
stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));
//注册自定义的C++类
register_all_mmo_api(stack->getLuaState());
//#if (COCOS2D_DEBUG > 0)
// NOTE:Please don’t remove this call if you want to debug with Cocos Code IDE
//startRuntime();
//#else
engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
//#endif
return true;
}
以上是代码,麻烦看下~ 多谢!
我是打的最终版本apk包 然后安装到手机上的
使用 package to apk 的方式其实就是 COCOS2D_DEBUG=0,不会增加任何调试相关的内容,这个时候帧率也很低?
我也是这么认为的 但安装好的包确实很低 只在3D模型半透移动的时候
切到后台 再切回游戏的时候 马上就又好了
基于lua-empty-test进行测试,修改creatDog()代码如下:
-- add the moving dog
local function creatDog()
local frameWidth = 105
local frameHeight = 95
-- create dog animate
-- local textureDog = cc.Director:getInstance():getTextureCache():addImage("dog.png")
-- local rect = cc.rect(0, 0, frameWidth, frameHeight)
-- local frame0 = cc.SpriteFrame:createWithTexture(textureDog, rect)
-- rect = cc.rect(frameWidth, 0, frameWidth, frameHeight)
-- local frame1 = cc.SpriteFrame:createWithTexture(textureDog, rect)
-- local spriteDog = cc.Sprite:createWithSpriteFrame(frame0)
-- spriteDog.isPaused = false
-- spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3)
–
local animFrames = CCArray:create()
animFrames:addObject(frame0)
animFrames:addObject(frame1)
]]–
-- local animation = cc.Animation:createWithSpriteFrames({frame0,frame1}, 0.5)
-- local animate = cc.Animate:create(animation)
-- spriteDog:runAction(cc.RepeatForever:create(animate))
local spriteDog = cc.Sprite3D:create("boss1.obj")
spriteDog:setScale(4.0)
spriteDog:setTexture("boss.png")
spriteDog:setPosition( origin.x, origin.y + visibleSize.height / 4 * 3 )
-- spriteDog:runAction(cc.RepeatForever:create(cc.RotateBy:create(3, 360)))
-- moving dog at every frame
local function tick()
if spriteDog.isPaused then return end
local x, y = spriteDog:getPosition()
if x > origin.x + visibleSize.width then
x = origin.x
else
x = x + 1
end
spriteDog:setOpacity(100)
spriteDog:setPositionX(x)
end
cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
return spriteDog
end
其中的boss1.obj的资源由cpp-tests拷贝,运行 cocos run -p android -j 4 -m release ,在小米3上测试,未出现顿卡。
能否提供重现环境。
你好,我问一下,我的打包操作是通过图形界面的方式直接生成的apk,名字为mmo-debug.apk
默认生成的还是调试版本么?是不是意味着mmo-debug.apk版本没有屏蔽掉apk和IDE之间的通信?导致游戏运行效率低下?
然后,我按照你命令行的方式 cocos run -p android -j 4 -m release
运行后结果报错:
running: ‘“C:\Cocos\tools\ant\bin\ant” clean release -f E:\GAME\Client\MMOGame\f
rameworks\runtime-src\proj.android\build.xml -Dsdk.dir=“D:\adt-bundle-windows-x8
6_64-20140702\sdk”’
系统找不到指定的路径。
Error running command, return code: 1
是SDK版本问题?我的是android-20?
请帮忙解决一下,不胜感激!
Lua 下 3D精灵半透明的效率肯定是没问题的,看下图:
最终经过测试和研究,效率问题解决了,给大家分享一下~
update中人物位置更新的逻辑虽然并没有很多计算的东西,但是却用到了lua中的位运算操作符bit_and和bit_or
这些lua层的计算导致了程序运行效率很低,我把bit_and和bit_or这些位的运算操作绑定到c++层,在lua中调用c++代码来替代lua的位运算,效率问题就解决了。
看来lua写写界面和简单的逻辑还好,涉及到大量运算的话 还是要小心了~