cocos2dx3.2 談談精靈也能加入觸摸事件回調函數簡單使用,呢嗎有木有更有的方法

在我點遊戲界面中放置兩個按鈕:Start, Exit,代表開始遊戲,和結束遊戲,如下:


看到了吧,妹妹不錯吧,嘿,百度搜的。。

那麼怎麼做到的呢?

首先導入資源ExitButton.jpg 和StartButton.jpg, 然後創建精靈如:

 auto start = Sprite::create("StartButton.jpg");

    start->setPosition(Vec2(start->getContentSize().width / 2 + 20, visibleSize.height / 2));

    addChild(start);

接下來給創建事件監聽器,並綁定回調函數:

auto startListener = EventListenerTouchOneByOne::create();

    startListener->onTouchBegan =[&](cocos2d::Touch* touch, cocos2d::Event* event)

    {

        cocos2d::Vec2 p = touch->getLocation();      

        

        auto target = event->getCurrentTarget();

        cocos2d::Rect rect = target->getBoundingBox(); //  判斷觸摸點是否在該按鈕裏面

        

        if(rect.containsPoint(p))

        {

            return true; // to indicate that we have consumed it.

        }

        

        return false; // we did not consume this event, pass thru.

    };

    startListener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)

    {

        

        StartLayer::StartGame(nullptr); //開始遊戲

        

    };

最後將監聽器加入事件分發器中,

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(startListener, start);

這樣就ok了,另一個退出按鈕的實現也按照同樣的方法。



這個是一般的方法,但是缺點比較明顯,就是添加了許多東西,接下來我要介紹另一種方法,我將其封裝到自定義到精靈中如下:

CustomSprite.h頭文件

//

//  CustomSprite.h

//  DontSaveMe

//

//  Created by Mr Gan on 12/23/14.

//

//


#ifndef __DontSaveMe__CustomSprite__

#define __DontSaveMe__CustomSprite__


#include "cocos2d.h"

#include <string>

#include <stdio.h>

#include <functional>

USING_NS_CC;


class CustomSprite : public Sprite

{

public:

    virtual ~CustomSprite(){}

    static CustomSprite* createWithPath(const std::string &path);

    

    std::function<bool(Touch*, Event*)> onTouchBegan;

    std::function<void(Touch*, Event*)> onTouchMoved;

    std::function<void(Touch*, Event*)> onTouchEnded;

    std::function<void(Touch*, Event*)> onTouchCancelled;

protected:

    CustomSprite():onTouchBegan(nullptr),onTouchEnded(nullptr) {}

    virtual bool initWithFile(const std::string& filename) override;

};





#endif /* defined(__DontSaveMe__CustomSprite__) */




//  CustomSprite.cpp

//  DontSaveMe

//

//  Created by Mr Gan on 12/23/14.

//

//


#include "CustomSprite.h"

 CustomSprite *CustomSprite::createWithPath(const std::string &path)

{

    auto sprite = new  CustomSprite();

    if(sprite && sprite->initWithFile(path))

    {

        sprite->autorelease();

        return  sprite;

    }

    else

    {

        delete sprite;

        sprite = nullptr;

        return nullptr;

    }

        

}



bool CustomSprite::initWithFile(const std::string& filename)

{

    if(!Sprite::initWithFile(filename))

    {

        return false;

    }

    

    auto listener = cocos2d::EventListenerTouchOneByOne::create();

    listener->setSwallowTouches(true);

    

    listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event)

    {

        cocos2d::Vec2 p = touch->getLocation();

        

        cocos2d::Rect rect = this->getBoundingBox();

        if(rect.containsPoint(p))

        {

            if(onTouchBegan)

                onTouchBegan(touch, event);

            return true; // to indicate that we have consumed it.

        }

        

        return false; // we did not consume this event, pass thru.

    };

    

    listener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)

    {

        if(onTouchEnded)

        onTouchEnded(touch, event);

    };

    

    cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

    

    return true;

    

}


怎麼用呢很簡單如下:


    auto start =  CustomSprite::createWithPath("StartButton.jpg");

    start->setPosition(Vec2(start->getContentSize().width / 2 + 20, visibleSize.height / 2));


    

    start->onTouchEnded = CC_CALLBACK_2(StartLayer::onBeginTouchStartButton, this);

    addChild(start);

    auto exit = CustomSprite::createWithPath("ExitButton.jpg");

    exit->setPosition(Vec2(start->getPositionX(), start->getPositionY() - 70));


    exit->onTouchEndedCC_CALLBACK_2(StartLayer::onBeginTouchExitButton, this);

    addChild(exit);


在定義onBeginTouchStartButton,和 onBeginTouchExitButton


void StartLayer::onBeginTouchStartButton(Touch* touch, Event* event)

{   

  StartGame(nullptr);

}


void StartLayer::onBeginTouchExitButton(Touch* touch, Event* event)

{

   ExitGame(nullptr);

}

這樣就ok啦,可別忘了在頭文件添加其引用,到此結束,希望能幫到一些小忙。















    







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章