facebook twitter hatena line google mixi email

Cocos2dx/画面遷移

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

画面遷移サンプル(android)

-Classes/HelloWorldScene2.h HelloWorldScene.hをコピーして作る

#ifndef __HelloWorldScene2_H__
#define __HelloWorldScene2_H__
#include "cocos2d.h"
class HelloWorldScene2 : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    void menuCloseCallback(cocos2d::Ref* pSender);
    CREATE_FUNC(HelloWorldScene2);
};
#endif // __HelloWorldScene2_H__

-Classes/HelloWorldScene2.cpp HelloWorldScene.cppをコピーして作る

#include "HelloWorldScene.h"
#include "HelloWorldScene2.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene* HelloWorldScene2::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorldScene2::create();
    scene->addChild(layer);
    return scene;
}
bool HelloWorldScene2::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorldScene2::menuCloseCallback, this));
    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
    auto label = Label::createWithTTF("Hello World2", "fonts/Marker Felt.ttf", 24);
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
    this->addChild(label, 1);
    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(sprite, 0);
    return true;
}
void HelloWorldScene2::menuCloseCallback(Ref* pSender)
{
    // 最初の画面に戻るように
    Director::getInstance()->pushScene(HelloWorld::createScene());
}

-Classes/HelloWorldScene.cpp

#include "HelloWorldScene2.h"
// 略
void HelloWorld::menuCloseCallback(Ref* pSender)
{
     // ボタンイベントあたりに画面遷移コードを記述
     Director::getInstance()->pushScene(HelloWorldScene2::createScene());
}

-proj.android-studio/app/jni/Android.mk LOCAL_SRC_FILESにHelloWorldScene2.cppを追加

LOCAL_SRC_FILES := hellocpp/main.cpp \
                  ../../../Classes/AppDelegate.cpp \
                  ../../../Classes/HelloWorldScene.cpp \
                  ../../../Classes/HelloWorldScene2.cpp

参考

https://iscene.jimdo.com/2015/02/05/cocos2d-x-3-x-%E7%B0%A1%E5%8D%98%E3%81%AA%E7%94%BB%E9%9D%A2%E9%81%B7%E7%A7%BB%E3%82%92%E4%BD%9C%E3%81%A3%E3%81%A6%E3%81%BF%E3%82%8B/