1
0

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でSceneを作成・切り替える方法

Last updated at Posted at 2018-02-19

新しいSceneクラスを作成します。
例ではCharacterListと言うキャラクターを表示するシーンの想定していきます。

CharacterList.h
#ifndef CharacterList_h
#define CharacterList_h

#include "cocos2d.h"

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



#endif
CharacterList.cpp
#include "CharacterList.h"

USING_NS_CC;


Scene* CharacterList::Scene(){
    auto scene = CharacterList::create();
    return scene;
}

bool CharacterList::init()
{
    if ( !Scene::init() ){
        return false;
    }
    
    
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    

    auto sprite = Sprite::create("Background.jpg");
    if (sprite == nullptr)
    {
        printf("Error");
    }
    else
    {
setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
        
        // add the sprite as a child to this layer
        this->addChild(sprite, 0);
    }
    
    

    return true;
}


CPP内のCharacterList::create();を実行する事で
CharacterListのinitが自動的に呼び出される様になってます。

大まかにこんな感じで準備したら、
切り替えたい箇所で次のコードを記述するだけ

Director::getInstance()->replaceScene(CharacterList::Scene());
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?