我目前正在使用cocos2dx。我的测试设备是ipad2我希望我的精灵大小独立于纹理大小,因为我无法提供一对一的映射。这是一个例子 假设我有一个尺寸为1024,1024的纹理 我想只使用从0,0到300,300的部分 我希望精灵从100,100到200,200画,我怎么能实现这个
我尝试过设置quad manualy但它给出了一个奇怪的行为 我的四边形顶点是
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 问候
答案 0 :(得分: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
,但它们不是自动释放。