cocos2d 单点触控

时间:2024-06-21 12:07:26
//
// Single.hpp
// dev
//
// Created by sun on 15/12/20.
//
// #ifndef Single_hpp
#define Single_hpp #include <stdio.h>
#include "cocos2d.h"
#include "HelloWorldScene.h"
USING_NS_CC;
class Single : public CCLayer
{
public:
virtual bool init();
static CCScene* scene();
CREATE_FUNC(Single); //touch
private:
void registerWithTouchDispatcher(void);
bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
}; #endif /* Single_hpp */
//
// Single.cpp
// dev
//
// Created by sun on 15/12/20.
//
// #include "Single.hpp" CCScene* Single::scene()
{
CCScene *scene = CCScene::create();
Single *layer = Single::create();
scene->addChild(layer);
return scene;
} bool Single::init()
{
if ( !CCLayer::init() )
{
return false;
} this->setTouchEnabled(true); return true;
} void Single::registerWithTouchDispatcher()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
} bool Single::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint touchpoint = pTouch->getLocation(); //获取触摸坐标
CCLOG("touch began, touchpoint is %f %f", touchpoint.x,touchpoint.y); CCScene* helloscene=HelloWorld::scene();
CCDirector::sharedDirector()->replaceScene(helloscene); return true; //true表示继续响应CCTouchMove,CCTouchEnd,CCTouchCancalled,false表示不响应。
} void Single::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint touchpoint = pTouch->getLocation(); //获取触摸坐标
CCLOG("touch moved, touchpoint is %f %f", touchpoint.x,touchpoint.y);
} void Single::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint touchpoint = pTouch->getLocation(); //获取触摸坐标
CCLOG("touch ended, touchpoint is %f %f", touchpoint.x,touchpoint.y);
} void Single::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint touchpoint = pTouch->getLocation(); //获取触摸坐标
CCLOG("touch cancelled, touchpoint is %f %f", touchpoint.x,touchpoint.y);
}