Sprite::createWithTexture Rect 未按预期工作
Sprite::createWithTexture with Rect not working as expected
我正在尝试从图像构建精灵。使用 Image 和 Texture2D class,然后从 texture2D 创建精灵。
我正在加载的图像是 512x512,我希望两个版本的 createWithTexture 表现相同,但事实并非如此。这里的代码:
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);
这里是第一个使用 Rect 的结果:
这里是没有 Rect 的第二个版本:
有人知道发生了什么事吗?我需要使用使用 rect 的方法,因为我将来会从这个图像创建一堆精灵。
Edit1:调试两个版本的精灵后。我注意到在没有 Rect 的情况下创建的那个显示了 0,0,240,240 的 rect。而不是我提供的 0,0,512,512。为什么是 240?
提前致谢。
如果您的 vecSize
比图片的尺寸大,图片就会变形。
所以如果你不知道图片的实际尺寸,就不要设置它。
我设法弄清楚发生了什么。 Cocos2D-x 使用 director->setContentScaleFactor 和 glview->setDesignResolutionSize 作为简化多 resolution/device 游戏的方法。当您构建 Rect 以获得部分(或完整)纹理时,您必须考虑 CC_CONTENT_SCALE_FACTOR() 宏,以便获得正确的目标坐标。
这可以在 link 处检查:http://www.cocos2d-x.org/wiki/Multi_resolution_support
干杯。
我正在尝试从图像构建精灵。使用 Image 和 Texture2D class,然后从 texture2D 创建精灵。
我正在加载的图像是 512x512,我希望两个版本的 createWithTexture 表现相同,但事实并非如此。这里的代码:
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);
这里是第一个使用 Rect 的结果:
这里是没有 Rect 的第二个版本:
有人知道发生了什么事吗?我需要使用使用 rect 的方法,因为我将来会从这个图像创建一堆精灵。
Edit1:调试两个版本的精灵后。我注意到在没有 Rect 的情况下创建的那个显示了 0,0,240,240 的 rect。而不是我提供的 0,0,512,512。为什么是 240?
提前致谢。
如果您的 vecSize
比图片的尺寸大,图片就会变形。
所以如果你不知道图片的实际尺寸,就不要设置它。
我设法弄清楚发生了什么。 Cocos2D-x 使用 director->setContentScaleFactor 和 glview->setDesignResolutionSize 作为简化多 resolution/device 游戏的方法。当您构建 Rect 以获得部分(或完整)纹理时,您必须考虑 CC_CONTENT_SCALE_FACTOR() 宏,以便获得正确的目标坐标。
这可以在 link 处检查:http://www.cocos2d-x.org/wiki/Multi_resolution_support
干杯。