cocos2dxで開発をしています。
変数を外部で処理したいと思い、次のコードを書きましたが、思うような結果が出てきません。
前提・実現したいこと
d_Cnt_Inside : 11 str_Enter_Inside : 12121221221
touchEvent内だとの望む結果は得られています。
発生している問題
これをdoOutsideで処理すると
d_Cnt_Outside : 1 str_Enter_Outside : 1
となってしまいます。どうも毎回リセットされてしまうようです。
変数の参照の使い方だと思いますが、どうも理解できないため、解決方法を教えていただけると有り難いです。
よろしくお願いいたします。
該当のソースコード
c++
1 2// Test.hpp 3class Test : public cocos2d::Layer 4{ 5public: 6 static cocos2d::Scene* createScene(); 7 virtual bool init() ; 8 9 string str_Enter_Inside; 10 int d_Cnt_Inside; 11 12 string str_Enter_Outside; 13 int d_Cnt_Outside; 14 15 void touchEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type); 16 void doOutside(string s_enter,int _cnt,int tag); 17 18 CREATE_FUNC(Test); 19};
c++
1// Test.cpp 2bool Test::init(){ 3 if(!Layer::init()) { return false; } 4 Vec2 origin = Director::getInstance()->getVisibleOrigin(); 5 for (int i=1; i<=2; i++) { 6 auto Btn=Button::create("_frame5.png","_frame5.png"); 7 Btn->setPosition(Vec2(origin.x +50*i,100)); 8 Btn->setTag(i); 9 Btn->setTitleText(format("%d",i)); 10 Btn->addTouchEventListener(CC_CALLBACK_2(Test::touchEvent, this)); 11 this->addChild(Btn); 12 } 13 return true; 14} 15 16void Test::touchEvent(Ref *pSender, Widget::TouchEventType type){ 17 Label* sprite = (Label*)pSender; 18 int tag = sprite->getTag(); 19 if (type== ui::Widget::TouchEventType::BEGAN) { 20 //この中で計算すると望む結果が出る 21 str_Enter_Inside = str_Enter_Inside+format("%d",tag); 22 d_Cnt_Inside++; 23 CCLOG("d_Cnt_Inside : %i",d_Cnt_Inside); 24 CCLOG("str_Enter_Inside : %s",str_Enter_Inside.c_str()); 25 26 //外で処理しようとすると 27 doOutside(str_Enter_Outside,d_Cnt_Outside,tag); 28 } 29} 30 31void Test::doOutside(string s_enter,int _cnt,int tag){//外での処理 32 // 本当はここで望む結果を得たい 33 s_enter = s_enter+format("%d",tag); 34 _cnt++; 35 CCLOG("d_Cnt_Outside : %i",_cnt); 36 CCLOG("str_Enter_Outside : %s",s_enter.c_str()); 37} 38
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/22 01:24