问题描述
- cocos2d中关于回调函数的pSender参数的问题 10C
- 运行结果显示,CallBack1和CallBack2产生的效果是一样的,请问CallBack2处的pSender的作用是什么,为什么Sprite* sp = (Sprite*)pSender;后sp与sprite等效了?
cpp中
void MyScene::goMenu(cocos2d::Ref* pSender){ log(""Tag=%i""this->getTag()); switch (this->getTag()) { case func: this->OnCallFunc(pSender); break; case funcN: this->OnCallFuncN(pSender); break; }}void MyScene::backMenu(Ref* pSender){ Scene* scene = HelloWorld::createScene(); Director::getInstance()->replaceScene(scene);}void MyScene::OnCallFunc(Ref* pSender){ FiniteTimeAction* ac1 = MoveBy::create(2Vec2(100100)); FiniteTimeAction* ac2 = ac1->reverse(); FiniteTimeAction* acf = CallFunc::create(CC_CALLBACK_0(MyScene::CallBack1this)); FiniteTimeAction* seq = Sequence::create(ac1acfac2NULL); log(""%p"" this->sprite); this->sprite->runAction(Sequence::create(seqNULL));}void MyScene::OnCallFuncN(Ref* pSender){ FiniteTimeAction* ac1 = MoveBy::create(2 Vec2(100 100)); FiniteTimeAction* ac2 = ac1->reverse(); FiniteTimeAction* acf = CallFuncN::create(CC_CALLBACK_1(MyScene::CallBack2 this)); FiniteTimeAction* seq = Sequence::create(ac1 acf ac2 NULL); sprite->runAction(Sequence::create(seqNULL));}void MyScene::CallBack1(){ sprite->runAction(TintBy::create(0.5 255 0 255));}void MyScene::CallBack2(Ref* pSender){ Sprite* sp = (Sprite*)pSender; sp->runAction(TintBy::create(0.5 255 0 255));}
头文件中
#pragma once#include ""cocos2d.h""using namespace cocos2d;class MyScene :public Layer{ Sprite* sprite;public: static Scene* createScene(); virtual bool init(); CREATE_FUNC(MyScene); void goMenu(Ref* pSender); void backMenu(Ref* pSender); void OnCallFunc(Ref* pSender); void OnCallFuncN(Ref* pSender); void CallBack1(); void CallBack2(Ref* pSender);};
解决方案
http://www.2cto.com/kf/201401/275831.html
时间: 2025-01-30 15:23:15