Memorandum

自分へのメモ。そして誰かの役に立てば、うれしい。

cocos2d-xでゲームを作ろう! 第二回 マップ表示編

1.プロジェクトを作成しよう!

さて、作成したマップを表示するわけですが、まずは、新規プロジェクトを作成します。
(テンプレートから「cocos2d-x」を選択してください)
f:id:Keizi:20130907154044p:plain
これを、そのままビルド&実行すると、こんな感じになります。簡単ですね。
f:id:Keizi:20130907154716p:plain

2.マップを表示してみよう!

次に前回紹介した「Tiled」のサンプルデータを使ってマップ表示してみたいと思います。

まず、以下の3つのファイルを作成したプロジェクトに追加します。
f:id:Keizi:20130907163558p:plain
準備はこれで完了!
あとは、「HelloWorld::init()」に表示用のコードを2行追加するだけで
読み込んだマップが表示されちゃいます。

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition( ccp(size.width/2, size.height/2) );

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    // マップチップ表示  ここから 
    CCTMXTiledMap* pTileMap = CCTMXTiledMap::create("desert.tmx");
    this->addChild(pTileMap);</span>
    // マップチップ表示  ここまで
    
    return true;
}

こんな感じです!なんて簡単な世の中なんでしょう!
あとは、ここにキャラクタを表示させて、マップをスクロールするようにすれば完成したも同然!なんてね。
次回は、キャラクタを表示させて、移動させちゃいましょうー。

f:id:Keizi:20130907162719p:plain

話かわりますが、「スーパーpre記法」中の文字の背景色を特定の部分だけハイライトとかできないんでしょうか。。。
追加箇所が分かりやすいように。ご存知の方がおられれば教えてください。