Search code examples
iosobjective-ccocos2d-iphone

Cocos2d Equivalent of Sprite Kit's size property


Hey I learned sprite kit before i went to Cocos2d but one thing I miss about sprite kit is how you can change the size of the sprite just by using cgSizeMake(x,y); . I can't seem to find the equivalent of where to use cgsizemake. I tried with contentSize but it didn't change anything and made it just move. I know setScale can change the size but setScale bases it off the initial size of the image and you just name the length and the width with setScale.Is there a equivalent of this in Cocos2d-swift or am i stuck with setScale.


Solution

  • Use getContentSize() to get the original size of the sprite and use that to setScale() to the size you need.

    Example:

    Full size: 200x200px
    Desired size: 20x20px
    sprite.setScale(20.0/sprite.getContentSize().width, 20.0/sprite.getContentSize().height);

    Edit: Also, you could subclass or create an extension to create a method called setSize() that does this.

    Edit 2: To get the size of the sprite after scaling, see here:
    How to get CCSprite's size after changing scale
    Also added another link to related for scaling a sprite to an exact size.

    Related: