0
2

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.

Sceneへの変数渡し[Cocos2d-x]

0
Last updated at Posted at 2017-11-23

はじめに

たぶん使いたくなる時があるはず
Cocos2d-xで使用できる便利なプログラムで書いたXTLayerを継承している

Sample.h
# ifndef __Sample__
# define __Sample__

# include "cocos2d.h"
# include "XTLayer.h"

class Sample : public XTLayer
{   
public:
    //setterとgetterを一気に作れる
    CC_SYNTHESIZE(int, _stage, Stage);
    CC_SYNTHESIZE(int, _level, Level);

    ~Sample();
    Sample();

    //元の記述
    /*
    virtual bool init() override;
    static cocos2d::Scene* scene();
    CREATE_FUNC(Sample);
    */

    //上部記述をコメントアウトして下記のように書く
    //渡したい変数を括弧の中に書く
    virtual bool init(int stageNo, int level);
    static cocos2d::Scene* scene(int stageNo, int level);
    static Sample* create(int stageNo, int level);
    
    //virtual void xtTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
    //virtual void xtTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
    //virtual void xtTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
    
    virtual void xtTouchesBegan(cocos2d::Vec2 position) override;
    virtual void xtTouchesMoved(cocos2d::Vec2 position) override;
    virtual void xtTouchesEnded(cocos2d::Vec2 position) override;
    
    virtual void xtTapGesture(cocos2d::Vec2 position) override;
    virtual void xtDoubleTapGesture(cocos2d::Vec2 position) override;
    virtual void xtLongTapGesture(cocos2d::Vec2 position) override;
    virtual void xtSwipeGesture(XTTouchDirection direction, float distance, float speed) override;

    
};

# endif /* defined(__Sample__) */

Sample.cpp
# include "Sample.h"

USING_NS_CC;

Sample::~Sample(){}
Sample::Sample(){}

Scene* Sample::scene(int stageNo, int level){
    Scene *scene = Scene::create();
    Sample *layer = Sample::create(stageNo, level);
	scene->addChild(layer);

    return scene;
}

Sample* Sample::create(int stageNo, int level){
    Sample* ret = new Sample();
    
    if (ret && ret->init(stageNo, level)){
        ret->autorelease();
        return ret;
    }else{
        CC_SAFE_DELETE(ret);
        return NULL;
    }
}

bool Sample::init(int stageNo, int level){
    
    if (!Layer::init()) return false;
    
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(Sample::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(Sample::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(Sample::onTouchEnded, this);
    listener->setSwallowTouches(true);
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    
    _stageNo = stageNo;
    _level = level;
    
    return true;
}

void Sample::xtTouchesBegan(Vec2 position){}
void Sample::xtTouchesMoved(Vec2 position){}
void Sample::xtTouchesEnded(Vec2 position){}
void Sample::xtTapGesture(Vec2 position){}
void Sample::xtDoubleTapGesture(Vec2 position){}
void Sample::xtLongTapGesture(Vec2 position){}
void Sample::xtSwipeGesture(XTTouchDirection direction, float distance, float speed){
    std::string directionStr = "";
    switch (direction) {
        case XTLayer::UP: directionStr = "UP"; break;
        case XTLayer::DOWN: directionStr = "DOWN"; break;
        case XTLayer::LEFT: directionStr = "LEFT"; break;
        case XTLayer::RIGHT: directionStr = "RIGHT"; break;
        default:break;
    }
    //log("xtSwipeGesture direction: %s, distance: %f, speed: %f", directionStr.c_str(), distance, speed);
}

後は受けた値を使ってステージや難易度により変化するプログラムを作る

次回

親レイヤーから子レイヤーへのコールバック渡し

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?