2012-12-11 21 views
3

我想要一個背景圖像repeat-x,但不想更改圖像的witdh和高度。cocos2d-x如何重複NPOT圖像

CCSize s = CCDirector::sharedDirector()->getWinSize(); 
    CCSprite* sprite = CCSprite::create("sprite.png"); // the image size is 256 * 224, so the height is non power of 2. 
    CCRect spriteRect = sprite->getTextureRect(); 
    spriteRect.size.width = s.width; 
    pSkyBg->setTextureRect(skyRect); 

    ccTexParams tp = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; 
    sprite->getTexture()->setTexParameters(&tp); 

    sprite->setPosition((ccp(0, s.height))); 
    sprite->setAnchorPoint(ccp(0, 1)); 
    addChild(sprite, 0); 

有一些錯誤。誰能幫我!謝謝!

回答

2

圖片的高度和寬度必須是2的冪。顯然224不是。

+0

我們不能用GL_WRAP? – Pradeep

1

它與2圖像的力量完美結合。

這裏我的代碼:

CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("purty_wood.png"); 
ccTexParams tp = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; 
texture->setTexParameters(&tp); 
CCSprite *background = CCSprite::createWithTexture(texture, CCRectMake(0, 0, visibleSize.width, visibleSize.height)); 
background->setPosition(ccp(visibleSize.width/2, visibleSize.height/2)); 
this->addChild(background, 1);