cocos2d-x如何重复使用一个非二的幂次方图片

3

我希望背景图片能够横向重复出现,但是不想改变这张图片的宽度和高度。

    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
图片的高度和宽度必须是2的幂次方。显然224不符合该要求。

我们不能使用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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接