I’m trying to build a sprite from an image. Using Image and Texture2D class and then later creating a sprite from the texture2D.
The image I am loading is 512x512 and I expected both versions of the createWithTexture to behave the same but they don’t. Here the code:
Image* image = new Image();
image->initWithImageFile(fileName);
Texture2D* texture = new Texture2D();
texture->initWithImage(image);
//If used this way everything works as expected
Sprite* spr= Sprite::createWithTexture(texture);
//If used with a Rect weird result occurrs.
Sprite* spr= Sprite::createWithTexture(texture,Rect(0,0,512,512));
spr->setAnchorPoint(Vec2(0,0));
spr->setPosition(Vec2(0,0));
spr->setScale(1.0f,1.0f);
this->addChild(spr);
You can check the result of both versions at stack overflow: http://stackoverflow.com/questions/31878291/spritecreatewithtexture-with-rect-not-working-as-expected
I have noticed that cocos2d-x uses wdith/CC_CONTENT_SCALE_FACTOR() and height/CC_CONTENT_SCALE_FACTOR() to crate the Rect for the sprite creation. Why is this? Could anybody explain a bit why aren’t you using just pixels for that? Isn’t a bit weird to pass width and height / CC_CONTENT_SCALE_FACTOR() when creating a sprite from a part of a texture?
P.D. I’m using cocos2d-x 3.7
Cheers.