c++ - Sprite::createWithTexture with Rect 未按预期工作

标签 c++ cocos2d-x

我正在尝试从图像构建 Sprite 。使用 Image 和 Texture2D 类,然后从 texture2D 创建 Sprite 。

我正在加载的图像是 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 的结果:

enter image description here

这里是没有 Rect 的第二个版本:

enter image description here

有人知道这是怎么回事吗?我需要使用使用 rect 的方法,因为我将来会根据这张图片创建一堆 Sprite 。

Edit1:在调试两个版本的 Sprite 之后。我注意到在没有 Rect 的情况下创建的那个显示了 0,0,240,240 的 rect。而不是我提供的 0,0,512,512。为什么是 240?

提前致谢。

最佳答案

我设法弄清楚发生了什么。 Cocos2D-x 使用 director->setContentScaleFactor 和 glview->setDesignResolutionSize 来简化多分辨率/设备游戏。当您构建 Rect 以获得部分(或完整)纹理时,您必须考虑 CC_CONTENT_SCALE_FACTOR() 宏,以便获得正确的目标坐标。

可以通过以下链接查看:http://www.cocos2d-x.org/wiki/Multi_resolution_support

干杯。

关于c++ - Sprite::createWithTexture with Rect 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31878291/

相关文章:

带有 std::basic_string<> g++ 6.3.0 的 C++11 状态分配器

c++ - system() 在作为 CGI 调用时总是返回非零值

android - 如何在 cocos2dx 中更新 openssl?

c++ - Texture2D : Error uploading compressed texture level: 0. glError: 0x0500

cocos2d-x - 嵌入式 cocos2d-js 应用程序可以回调 C++ 吗?

c++ - 如何通过命名空间调用非静态方法

c# - 不同 K 和 Volume 之间的 K-Dop 碰撞

c++ - 图像卷积与高斯模糊,加速可能吗?

c++ - 反转for循环导致系统错误

c++ - 具有 FPS 校正功能的 cocos2d 回放系统