游戏在主循环中增加了一个复杂的碰撞检查后,开始出现掉帧,怪物元素量大时,原本60的fps只剩20~30之间;
引用cocos2d官方的一段话:
Basically all the cocos2d-iphone, Chipmunk or CocosBuilder Reader APIs are going to perform almost at native speed. But you should pay attention to the following scenarios:
The performance could be slow down while the garbage collector is run. Workaround: Do not create many JS objects. Reuse as many as possible
Having a complex main loop could slow down the performance. Workaround: Profile your JS code, and if you can't further optimize it, write the expensive parts in Objective-C and create JS bindings for those functions.
掉帧现象部分来自于主循环运算量过大,60fps的游戏, 主循环每秒运算60次, 可以减少主循环中不必要的运算;
怪物每帧移动,施法,判断碰撞等,可以改为每5帧判断一次,运算量会小很多;
具体的做法,是在主循环中获取当前时间戳,运行动作后记录时间戳, 下次运行时当前时间和最后记录的时间戳达到一定时间以上,再进行动作;
本次调整后的游戏表现及体验均未有影响,fps又稳定在58以上;