Hi, everyone!
Have a trouble with pausing a game.
By game logic at an end of the game I need to stop all actions exclude music and sounds and draw the score dialog.
don’t use cc.director.pause() to pause your game , instead for example you can make an cc.Enum and add your game states in there , this way you can have a game state variable and change it to different states. like this :
It sounds like you may be trying to pause the game while running an animation of some sort to show your winDialog. I do something similar in my game where I want to pause the “game” portion of my code but keep the UI portion animating and responsive to input.
What I do is create a “gameRoot” node and a “uiRoot” node at the top level. Everything under the gameRoot will be paused when I hit the pause button, but my menus and other stuff still work and have nice animations (under the uiRoot node).
I’m not sure if it is necessary, but I found that it works best if I recursively pause all of the nodes under my “gameRoot” using functions like these:
pauseNodeAndDescendants: function(node) {
node.pauseAllActions();
var that = this;
node.children.forEach(child => {
that.pauseNodeAndDescendants(child);
});
},
resumeNodeAndDescendants: function(node) {
node.resumeAllActions();
var that = this;
node.children.forEach(child => {
that.resumeNodeAndDescendants(child);
});
},
So when I hit my pause button, I call pauseNodeAndDescendants on gameRoot and when I unpause I call resumeNodeAndDescendants on gameRoot.
it’s exactly what I need.
I was think that there is more elegant move in Cocos API. But it is not.
Does forEach cycle normal from point of view a processor load?
Is your code quick or it can implemented better?
Node system events includes touch and mouse events. Because this object reacts on a screen touches.
I was create an additional Class named Stoppable and implement it by my scripts. It has pause() and resume().
In button’s handler:
this.node.children.forEach(child => {
if (child.getComponent('Stoppable') !== null) child.getComponent('Stoppable').pause()
})
I’m not 100% sure but a “regular” for loop over the children array may be faster. On my relatively small scene-graphs (a few dozen nodes) I have tested my code in the simulator and on an 3rd generation iPad and an iPhone 5 and there is no perceivable delay between hitting the pause button and everything stopping.