LoginSignup
8
8

More than 5 years have passed since last update.

cocos2d-x入門 -3-

Last updated at Posted at 2013-11-10

画像付きのスプライトを表示する

Spriteを使ってシーンに画像スプライトを表示します。

画像はこちらの将棋のものにしてみました。
将棋画像
ダウンロードしたzipファイルを展開すると"japanese-chess-b02.jpg"(幅600×高さ640)が入っていますので、これをXcodeのResourcesフォルダにドラッグ&ドロップで追加します。

画面解像度に合わせていい感じに表示されるようにsetContentScaleFactorメソッドを使います。

AppDelegate.cpp
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto eglView = EGLView::getInstance();

    director->setOpenGLView(eglView);

    // 将棋盤の高さ640にあわせてみる.
    director->setContentScaleFactor(640.0 / director->getWinSize().height);

GameSceneシーンの背景として画像を表示してみます。

GameScene.cpp
bool GameScene::init()
{
    if (! CCLayer::init()) {
        return false;
    }
    // 背景を作成する.
    makeBackground();
    return true;
}

// 背景を作成する.
void GameScene::makeBackground()
{
    Size winSize = Director::getInstance()->getWinSize();
    Sprite *spBG = Sprite::create("japanese-chess-b02.jpg");
    spBG->setPosition(Point(winSize.width * 0.5, winSize.height * 0.5));
    this->addChild(spBG);
}

実行すると、このようになります。

実行結果

今回はここまで。

8
8
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
8
8