Search code examples
cyclecocos2d-x

runAction() is called after cycle. is it ok?


here's sample on lua

for i=1,n do
    cclog(""..i)
    someItem:runAction(CCCallFunc:create(someCallback))
end

so first in console log it prints n lines with i and only then runs runAction n times so debug output looks like

1
2
3
4
...
n
someCallback
someCallback
someCallback
someCallback
...
someCallback

is it kind of memory leak protection or is it a bug


Solution

  • It's not a bug. What happens is that the actions you create and run when you call

    someItem:runAction(CCCallFunc:create(someCallback))
    

    are added to action manager of someItem. This happens for all actions that are run on nodes. By default, the action manager for a new node is taken from the director, thus all nodes by default share the same action manager. Nothing more than this happens during the runAction call. These actions are later updated when the action manager is updated. The action manager in the director is set to be updated once each frame. It's during this update that you get your "someCallback" output and this is the reason the output comes after the output of cclog(""..i).

    The CCCallFunc actions are removed right after they are run. Some other actions, like for example CCMoveTo, are kept in the action manager for a longer period of time. During every update of the action manager, the actions themselves are updated and checked if they should be removed. What CCCallFunc does is that it calls it's callback when it is updated and then say it's finished when the action manager asks. Thus, the callback is only called once per action and the action is then removed.

    Hope this helps. :)