1

我目前正在使用 cocos2dx。我的测试设备是 ipad2 我希望我的精灵大小独立于纹理大小,因为我负担不起一对一的映射。这是示例假设我有一个尺寸为 1024,1024 的纹理我只想使用它的一部分从 0,0 到 300,300 我希望该精灵从 100,100 到 200,200 绘制我怎么能做到这一点

我试过手动设置四边形,但它给出了一个奇怪的行为,我的四边形顶点是

     quad.bl.vertices = 0,0
     quad.br.vertices = 1024,0
     quad.tr.vertices = 1024,1024
     quad.tl.vertices = 0,1024

屏幕上不显示任何内容

但是当我将它们更改为

     quad.bl.vertices = 0,0
     quad.br.vertices = 2048,0
     quad.tr.vertices = 2048,2048
     quad.tl.vertices = 0,2048

然后部分精灵显示在屏幕上。在稍微更改顶点之后,似乎 coocs 2d 假设 ipad 的左下角位于 1024,1024 问候

4

1 回答 1

2

我认为这就是你需要的

CCRect requiredPortion = CCRectMake(0, 0, 300, 300);
CCRect drawArea = CCRectMake(100, 100, 100, 100);

CCImage *image = new CCImage();
image->initWithImageFile("texture.png");

//Create a full size texture for example 1024,1024
CCTexture2D *texture = new CCTexture2D();
texture->initWithImage(image);

//Create a sprite from the require portion of texture (Here size of created sprite will be 300,300);
CCSprite *spriteNew = CCSprite::createWithTexture(texture, requiredPortion);

//Setting anchor point to make sure we are drawing from required position on screen
spriteNew->setAnchorPoint(ccp(0, 0));

//Set scale to sprite to make it fit in you required area i.e (requiredSize/currentSize)
spriteNew->setScale(drawArea.size.width/requiredPortion.size.width);
spriteNew->setPosition(drawArea.origin);

someNode->addChild(spriteNew);

确保您删除image以后texture,它们不是自动释放的。

于 2013-08-29T06:52:31.860 回答