Hey,
Is it not possible to pause a cocos2d::Node directly in its init(), so it’s already paused when created?
Hey,
Is it not possible to pause a cocos2d::Node directly in its init(), so it’s already paused when created?
What do you mean by “pause” here? A Node, by default, is not running until you add it to a parent, after which it will execute “onEnter”. If you do not want it to execute its default onEnter(), then you will need to override the onEnter() function and set a flag in its init().
Example:
bool ChildNode::init()
{
flag = true;
...do something then return a bool value
}
void ChildNode::onEnter()
{
if (flag)
{
Node::onEnter();
}
}
bool ChildNode::init()
{
pause();
}
thats what i mean.
so it won’t be updated anymore
like i said, when you init a node, it is not updating. A node will only update and render if you add it to a parent. If you want it to render but not updating, then my method is the best you can do.
When you add a node to a parent, it will call its onEnter()
method to set its _running
flag. As long as this flag is false, it will not update the node.
Hope the explanation is clear for you.
but then i wont update at all right?,
the main idea is, i want the node to be pause when its created, and resume() it later.
I had no idea where it’s set to running by default.
like i said, you can override the onEnter() of your child node class to prevent it from updating. From what you said, your pause() should be something like that:
void ChildNode::init()
{
pause();
return true;
}
void ChildNode::pause()
{
pauseFlag = true;
if (_running) //check if it is in running state.
{
onExit(); //you want to set its running state to false
}
}
void ChildNode::onEnter()
{
if (!pauseFlag)
{
Node::onEnter();
}
}
void ChildNode::resume()
{
pauseFlag = false;
if (!_running)
{
onEnter();
}
}
Hope this helps.