1. 调用场景的pause方法
首先看一下pause方法的介绍,这是继承自Node节点的函数
* Pauses all scheduled selectors, actions and event listeners.
* This method is called internally by onExit.
这个方法只能够暂停节点本身的动作、监听器和update系列的函数。因此调用场景的pause方法后,场景中添加的精灵等还会继续运动,并不能达到我们想要暂停画面的效果。
2.调用Director的pause方法
/** Pauses the running scene.
* The running scene will be _drawed_ but all scheduled timers will be paused.
* While paused, the draw rate will be 4 FPS to reduce CPU consumption.
*/
结果是所有的update系列函数都暂停了,但是帧率只是变成了每秒四帧,并没有完全停止。。。精灵还是会动
3.调用Director的stopAnimation方法
/** Stops the animation. Nothing will be drawn. The main loop won't be triggered anymore.
* If you don't want to pause your animation call [pause] instead.
*/
调用后不再绘制新图像,即画面被“冻住”了,这时候到达了我们暂停的目标
“解冻”的方法是相应的startAnimation
/** The main loop is triggered again.
* Call this function only if [stopAnimation] was called earlier.
* @warning Don't call this function to start the main loop. To run the main loop call runWithScene.
*/
这两个方法基本上就可以实现我们的要求
但是这时会有一个问题,因为暂停之后不会再有图像绘制,所以当按下“恢复”的按钮或者其他控件时,不会绘制出这些控件“按下的效果”(比如按钮按下时的阴影效果),当然这也不是大问题,凑活就能用。
4. 自己编写暂停函数
当我们想要解决第三个办法中的小缺点,或者有特殊的实现(比如暂停时精灵停止运动,但背景不会停下),这时候就可以编写自己的pause函数,在这个函数中需要实现的很简单,就是调用所有需要暂停的节点的pause函数(第一个办法中提到的),然后还可以卸载某些事件监听器。