1. 程式人生 > >cocos2d-x 顯示觸控操作(顯示水波點選效果,用於視訊演示)

cocos2d-x 顯示觸控操作(顯示水波點選效果,用於視訊演示)

昨天剛剛參加玩遊戲設計大賽, 積累了一些東西。接下去將會逐個分享出來。

首先是顯示觸控操作

因為要演示我們的作品。使用試玩過程中, 如果沒辦法顯示我們的觸控操作(像錄製視訊一樣, 點選了螢幕某點, 出現紅點或者水波盪漾這樣的效果), 那樣的話演示效果不好。觀眾就無法直觀的瞭解我們的遊戲。所以考慮加入這個功能。

之後, 走了點彎路。一直在考慮手機本身有沒有這個功能,後來找了很久。非越獄iPhone是沒有這個功能的。

於是乎, 自己寫唄。

具體效果如下:


實現很簡單,主要用到了一個粒子效果。

具體步驟如下:

0.匯入粒子效果檔案. showClick.png + showClick.plist(可在我給出的demo中下載)

1.開啟觸控

2.在ccTouchBegan中獲取觸控點

3.在該觸控點中新增粒子效果

好了。下面給出具體程式碼。

當然, 也可以去我的Github中下載原始碼:

程式碼如下:

HelloWorld.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
using namespace cocos2d;

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();

    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);

    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
    
    
    //進入, 退出響應
    virtual void onEnter();
    virtual void onExit();
    
    //觸屏邏輯函式
    virtual void registerWithTouchDispatcher(void);
    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorld.m
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}


// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    
    return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}


#pragma mark - enter,exit
//進入響應函式
void HelloWorld::onEnter()
{
    CCLayer::onEnter();
    //進入開啟觸控
    this->setTouchEnabled(true);
}
//退出響應函式
void HelloWorld::onExit()
{
    CCLayer::onExit();
}

#pragma mark - 觸控事件

void HelloWorld::registerWithTouchDispatcher()
{
	//kCCMenuHandlerPriority=-128,將這個值設定為-128的二倍,可以比下邊的層的優先順序高
	//而且ccTouchBegan的返回值為true,說明其他的層將接受不到這個觸控訊息了,只有這個層上邊的
	//選單的優先順序比他還要打,所以它上邊的選單是可以接收到觸控訊息的
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,
                                                                            kCCMenuHandlerPriority*2,true);
}
//觸控事件
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    //獲得觸控點座標
    CCPoint touchLocation = pTouch->getLocation();
    
    CCParticleSystemQuad *mParticle =  CCParticleSystemQuad::create("showClick.plist");
    mParticle->setScale(0.5f);
    mParticle->setPosition(touchLocation);
    //如果不設定,粒子播放後記憶體不釋放
    mParticle->setAutoRemoveOnFinish(true);
    this->addChild(mParticle);
    
    return false;
}


學習的路上, 與君共勉。