cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.24k stars 7.06k forks source link

Node::addComponent would call excessive scheduleUpdate when adding multiple components #18812

Open stephenhurry opened 6 years ago

stephenhurry commented 6 years ago
bool Node::addComponent(Component *component)
{
    // lazy alloc
    if (!_componentContainer)
        _componentContainer = new (std::nothrow) ComponentContainer(this);

    // should enable schedule update, then all components can receive this call back
    scheduleUpdate();

    return _componentContainer->add(component);
}

The scheduleUpdate should be called once only, subsequent calling it would lead to a warning from Scheduler. So the snippet should be corrected as:

bool Node::addComponent(Component *component)
{
    // lazy alloc
    if (!_componentContainer)
    {
        _componentContainer = new (std::nothrow) ComponentContainer(this);

        // should enable schedule update, then all components can receive this call back
        scheduleUpdate();
    }

    return _componentContainer->add(component);
}
Dimon4eg commented 6 years ago

You can send PR with fix