今天我接着学习屏幕操作的响应事件。
Cocos2D-x的屏幕触摸主要有四个函数如下所示:
virtual void ccTouchesBegan(CCSet *pset,CCEvent *event);
virtual void ccTouchesMoved(CCSet *pset,CCEvent *event);
virtual void ccTouchesEnded(CCSet *pset,CCEvent *event);
virtual void ccTouchesCancelled(CCSet *pset,CCEvent *event);
次四个函数的功能从函数名很容易可以看出来,如过有什么疑问可以看专业文档,我就不做详细的描述了。。。。哈哈,反正我能看懂。
直接上代码吧:
HelloWorld头文件如下定义:
#include "SimpleAudioEngine.h"
using namespace cocos2d;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
virtual void ccTouchesBegan(CCSet *pset,CCEvent *event);
virtual void ccTouchesMoved(CCSet *pset,CCEvent *event);
virtual void ccTouchesEnded(CCSet *pset,CCEvent *event);
virtual void ccTouchesCancelled(CCSet *pset,CCEvent *event);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // _
HelloWorld实现文件如下:
bool HelloWorld::init()
{
bool bRet = false;
do
{
this->setTouchEnabled(true);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
void HelloWorld::ccTouchesBegan(CCSet *pset,CCEvent *event)
{
CCLOG("ccTouchesBegan");
}
void HelloWorld::ccTouchesMoved(CCSet *pset,CCEvent *event)
{
CCLOG("ccTouchesMoved");
}
void HelloWorld::ccTouchesEnded(CCSet *pset,CCEvent *event)
{
CCLOG("ccTouchesEnded");
}
void HelloWorld::ccTouchesCancelled(CCSet *pset,CCEvent *event)
{
CCLOG("ccTouchesCancelled");
}
当我们触摸屏幕时,当然我们这里时鼠标点击时输入如下:
ccTouchesEnded
ccTouchesBegan
ccTouchesMoved
ccTouchesMoved
ccTouchesMoved
ccTouchesMoved
ccTouchesMoved
ccTouchesEnded