Create a container to hold a sprite (or set view size for a sprite)

I have a sprite size 200x200px. But i only want to display it with size 150x150px and this sprite have some invisible zone.

In Corona SDK i can use display.newContainer() and i don’t know cocos2dx have this feature?
Someone can help me. Thank you!

you can do this:
sprite->setContentSize(Size(150, 150));

@Argas I don’t think setContentSize will change the display of Sprite.

we could display part of sprite by calling:

  sprite->setTextureRect(Rect(0,0,150,150));

I try:

  1. If I use setContentSize() -> Sprite only change content size, and change position
  2. If I use setTextureRect():
    setTextureRect(Rect(0, 0, width, height))
    -> It return a different sprite (not is a sprite i need)

auto sprite = Sprite::createWithSpriteFrameName(frameName); this->addChild(sprite); this->setPosition(Vec2(origin.width * 0.5f, origin.width * 0.5f)); auto rect = sprite->getTextureRect(); sprite->setTectureRect(Rect(rect.origin.x, rect.origin.y, false, Size(50, 50)));

It return for me a sprite with size 50x50px.

But i don’t think it is perfect.

I wonder cocos2dx have an api: setHeight(), setWidth()?
And Cocos2dx have an action to change size?

i am not sure that i understood what u need.
setContentSize() - yes it change only size of container (Sprite in your case). It doesn’t change sprite’s texture size and position of sprite. Your sprite changed position visually only, cause his anchor point was in center.

setTextureRect - this method cut your texture (and content size too);

if you want to change scale of image you can use SetScale method. It accept value from 0(0% scale) to 1 (100% scale). You can also set different scale to height and width of sprite.

I hope it will help you.

You can see this picture. It like container in ScrollView: when you setContainerSize.

Did you try to use glScissor to set View area?
I have used it and work fine but not sure if you have a lot of sprites.

well we can use masking here too. I wrote a blog regarding the same.
http://techbirds.in/how-to-do-masking-in-cocos2d-x/
. Hope it helps somewhat :slight_smile:

1 Like

Thank you so much. And i am thinking about action to change size when masking.