这是.h文件
#ifndef RECTLAYER
#define RECTLAYER
#include "cocos2d.h"
USING_NS_CC;
class RectLayer:public CCLayer
{
public:
RectLayer();
virtual void draw();
virtual void onEnter();
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
protected:
private:
};
#endif
```
这是.cpp文件
#include "RectLayer.h"
void RectLayer::draw()
{
ccDrawColor4B(255,0,0,255);
ccDrawRect(CCPointMake(0,0), CCPointMake(150,150));
}
RectLayer::RectLayer()
{
}
bool RectLayer::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
ccDrawColor4B(0,255,0,255);
ccDrawRect(CCPointMake(50,50), CCPointMake(150,150));
return false;
}
void RectLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint tPoint = pTouch->getLocation();
this->setPosition(tPoint.x, tPoint.y);
}
void RectLayer::onEnter()
{
this->setTouchEnabled(true);
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
}
```
我加了断点,这个layer是进入到onEnter()的,但是接收不到touch事件,求解啊!