关于回调函数(带参数的回调函数)

  • Post author:
  • Post category:其他


class PropLayer : public CCLayer
{
public:
	PropLayer(void);
	virtual ~PropLayer(void);
	virtual void Init(UIButton* _PropButton, int _iPropNum)=0;

	void TouchEvent(CCObject *pSender, TouchEventType type);

	CCObject* m_objListener;//对象
	SEL_CallFuncO m_funSelector;//函数
	void SetPropCallback(CCObject* obj,SEL_CallFuncO callfunc);//按下道具的回调(用于状态机的修改)

protected:
	UIButton *	m_PropButton;//道具按钮
	int			m_iPropNum;//道具数量
};
void PropLayer::TouchEvent( CCObject *pSender, TouchEventType type )
{
	switch (type)
	{
	case TOUCH_EVENT_BEGAN://按下
		if(m_objListener && m_funSelector)
		{
			CCString* str=CCString::createWithFormat("%d",1321);
			(m_objListener->*m_funSelector)(str);
		}
		CCLOG(CCString::createWithFormat("Touch Down")->getCString());
		break;

	case TOUCH_EVENT_MOVED://移动
		CCLOG(CCString::createWithFormat("Touch Move")->getCString());
		break;

	case TOUCH_EVENT_ENDED://抬起
		CCLOG(CCString::createWithFormat("Touch Up")->getCString());
		break;

	case TOUCH_EVENT_CANCELED://取消
		CCLOG(CCString::createWithFormat("Touch Cancelled")->getCString());
		break;

	default:
		break;
	}
}

void PropLayer::SetPropCallback( CCObject* obj,SEL_CallFuncO callfunc )
{
	m_objListener = obj;
	m_funSelector = callfunc;
}

m_RocketLayer = new RocketLayer;
	m_RocketLayer->Init(btRocket,1);
	m_RocketLayer->SetPropCallback(this,callfuncO_selector(LayerStage::PropLayerCallback));

void LayerStage::PropLayerCallback( CCObject * obj )
{
	CCString* str=(CCString*)obj;
	int value = str->intValue();
}

以上是带参数的回调方法,不带参数的回调如下:


SEL_CallFunc

m_funSelector;//函数指针

void SetRunGameCallback(CCObject* obj,

SEL_CallFunc

callfunc);



版权声明:本文为cuijiahao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。