Cocos2d-x_单点触摸

前端之家收集整理的这篇文章主要介绍了Cocos2d-x_单点触摸前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
#include "TestLayer.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);

    // CCLayer已经实现CCTouchDelegate协议,所以CCLayer的子类不用再次调用此接口
    // 重写“单点”触摸回调函数
    virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
    virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);
    virtual void ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent);
    
    // 重写生命周期函数
    virtual void onEnter();
    virtual void onExit();
    
};

#endif

//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
    return true;
}

void HelloWorld::onEnter()
{
    CCLayer::onEnter();
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,false);  // 增加“单点”触摸代理
    
    /*
     使用导演获得触摸事件调度者,给它添加一个代理,代理为this
     */
}

void HelloWorld::onExit()
{
    CCLayer::onExit();
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
    
    /*
     使用导演获得触摸事件调度者,把当前的代理this删除掉
     */
}

bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
{
    CCLOG("HelloWorld::ccTouchBegan");
    return true;
}

void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
{
    CCLOG("HelloWorld::ccTouchMoved");
}

void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
{
    CCLOG("HelloWorld::ccTouchEnded");
}

void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
{
    CCLOG("HelloWorld::ccTouchCancelled");
}
原文链接:https://www.f2er.com/cocos2dx/346760.html

猜你在找的Cocos2d-x相关文章