Search code examples
iosobjective-csprite-kitsknode

How does SKAction scaleBy:duration: method works under the hood?


Need to have a way to "scale by" without using an SKAction, but it should mimic changes to SKNode as scaleBy:duration: method does.

How does scale factor changes on the node which was "scaledBy"?


Solution

  • My guess is that there's some kind of action manager running its own update: method (like what SKScene has) and updates the appropriate properties (in this case xScale and yScale) of the node attached to the action on each callback.

    The update: method takes an NSTimeInterval parameter that can be used to know how far along the animation to progress and when to finish.

    EDIT

    In cocos2d-x, the CCScaleBy action is a subclass of CCScaleTo. The update method goes something like this:

    void CCScaleTo::update(float time)
    {
        setScaleX(m_fStartScaleX + m_fDeltaX * time);
        setScaleY(m_fStartScaleY + m_fDeltaY * time);
    }