setContentSIze in cc.Sprite doesn't set the size ,instead only changes the Position of the sprite

Hello,

My code is :

var image = cc.Sprite.create(“ball.png”);
image.setPosition(500,500);
image.setContentSize(cc.size(500,500));

The “setContentSize()” in cc.Sprite class doesn’t change the width and height of the image.
Instead it only changes the position of the image on screen.
Is this a bug or the functionality /meaning is different.

Thanks in advance.

1 Like

Hi, contentsize refers to the original size, if you want to resize the sprite, use setScale() instead

Hao Wu wrote:

Hi, contentsize refers to the original size, if you want to resize the sprite, use setScale() instead

But, if I want to reuse the CCSprite, as changing a texture.
I know setTexture will do the job, but if the new texture’s size is different from the original one, after setTexture the sprite node was looked freak.

1 Like

I want to set the size of a Sprite to a fixed pixel width/height. It seems like the method to do that doesn’t exist, but we have setScale. Is the following the best approach to setting the size of a Sprite to fixed pixel dimensions or is there a better way? It seems works, but it seems like a hack and I feel like I’m missing some more straightforward way of doing this.

Size tTargetSize = Size( 100.0f, 200.0f );
Size tSizeOrig = tSprite->getContentSize();
tSprite->setScale( ( tTargetSize.width / tSizeOrig.width ), ( tTargetSize.height / tSizeOrig.height ) );

1 Like

Taking it one step further, accounting for scale:

 front_sprite->setScale(4.0f);

 Size front_size = front_sprite->getContentSize();
 float front_x = front_sprite->getScaleX();
 float front_y = front_sprite->getScaleY();
 Size back_size = back_sprite->getContentSize();

 back_sprite->setScale(
     front_size.width*front_x/back_size.width,
     front_size.height*front_y/back_size.height
     );