10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cocos2d-xで画像をシーンの背景にリピート表示

10
Last updated at Posted at 2014-07-04

タイトルのままです。背景をタイルのようにrepeatで敷き詰めたい時に。
(注) 背景にしたい画像は2の階乗のサイズである必要があります。

Scene.cpp
bool Scene::init()
{
    //////////////////////////////
    
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
 
    // Set an image to a texture, set the param "repeat"
    Texture2D *bgTexture = Director::getInstance()->getTextureCache()->addImage("bg.jpg");
    const Texture2D::TexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
    
    // use the texture as Sprite
    Sprite *background = Sprite::createWithTexture(bgTexture, Rect(0, 0, visibleSize.width, visibleSize.height));
    background->getTexture()->setTexParameters(tp);
    background->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
    this->addChild(background, 1);
    
    :
    :
10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?