LoginSignup
6
6

More than 5 years have passed since last update.

cocos2d-x入門 -2-

Last updated at Posted at 2013-11-04

シーンを新規作成する

プロジェクトを作成すると最初は"Hello World"と表示されるシーンが作成されますが、自分で作成するシーンに差し替えましょう。

まずは、C++クラスを追加します。

C++クラス追加

クラス名はGameSceneにしました。
Classesフォルダにフィアルを追加していきましょう。

GameScene作成

GameScene.hはこのように実装します。

GameScene.h
#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__

#include "cocos2d.h"

class GameScene : public cocos2d::Layer
{
public:
    static cocos2d::Scene *createScene();
    virtual bool init();
    CREATE_FUNC(GameScene);
};

#endif // __GAME_SCENE_H__

つづいて、GameScene.cppはこのように実装します。

GameScene.cpp
#include "GameScene.h"

using namespace cocos2d;
using namespace std;

Scene *GameScene::createScene()
{
    Scene *scene = Scene::create();
    GameScene *layer = GameScene::create();
    scene->addChild(layer);
    return scene;
}

bool GameScene::init()
{
    if (! CCLayer::init()) {
        return false;
    }
    return true;
}

最後に、AppDelegate.cppでHelloWorldSceneを使用しているところをGameSceneに置き換えましょう。

AppDelegate.cpp
#include "AppDelegate.h"
#include "GameScene.h"

()

    // create a scene. it's an autorelease object
    auto scene = GameScene::createScene();

()

実行すると、真っ黒い画面になります。

実行結果

HelloWorldScene.hとHelloWorldScene.cppは不要になるので削除してしまってかまいません。

今回はここまで。

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