cocos2dx3.2 谈谈精灵也能加入触摸事件回调函数简单使用,呢吗有木有更有的方法

前端之家收集整理的这篇文章主要介绍了cocos2dx3.2 谈谈精灵也能加入触摸事件回调函数简单使用,呢吗有木有更有的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在我点游戏界面中放置两个按钮:Start,Exit,代表开始游戏,和结束游戏,如下:


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

那么怎么做到的呢?

首先导入资源ExitButton.jpg 和StartButton.jpg, 然后创建精灵如:

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

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

@H_301_29@ addChild(start);

@H_301_29@接下来给创建事件监听器,并绑定回调函数

@H_301_29@

auto startListener = EventListenerTouchOneByOne::create();

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

@H_301_29@ {

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

@H_301_29@ auto target = event->getCurrentTarget();

@H_301_29@ cocos2d::Rect rect = target->getBoundingBox(); // 判断触摸点是否在该按钮里面

@H_301_29@ if(rect.containsPoint(p))

@H_301_29@ {

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

@H_301_29@ }

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

@H_301_29@ };

@H_301_29@

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

@H_301_29@ {

@H_301_29@ StartLayer::StartGame(nullptr); //开始游戏

@H_301_29@ };

最后将监听器加入事件分发器中,

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

@H_301_29@这样就ok了,另一个退出按钮的实现也按照同样的方法

@H_301_29@

@H_301_29@

@H_301_29@这个是一般的方法,但是缺点比较明显,就是添加了许多东西,接下来我要介绍另一种方法,我将其封装到自定义到精灵中如下:

@H_301_29@

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;


@H_301_29@class CustomSprite : public Sprite

@H_301_29@{

public:

@H_301_29@ virtual ~CustomSprite(){}

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

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

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

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

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

protected:

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

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

};





#endif /* defined(__DontSaveMe__CustomSprite__) */




// CustomSprite.cpp

//


#include "CustomSprite.h"

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

@H_301_29@{

@H_301_29@ auto sprite = new CustomSprite();

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

@H_301_29@ {

@H_301_29@ sprite->autorelease();

@H_301_29@ return sprite;

@H_301_29@ }

@H_301_29@ else

@H_301_29@ {

@H_301_29@ delete sprite;

@H_301_29@ sprite = nullptr;

return nullptr;

@H_301_29@ }

@H_301_29@}



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

@H_301_29@{

@H_301_29@ if(!Sprite::initWithFile(filename))

@H_301_29@ {

return false;

@H_301_29@ }

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

@H_301_29@ listener->setSwallowTouches(true);

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

@H_301_29@ {

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

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

@H_301_29@ if(rect.containsPoint(p))

@H_301_29@ {

@H_301_29@ if(onTouchBegan)

@H_301_29@ onTouchBegan(touch,event);

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

@H_301_29@ }

301_29@ };

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

@H_301_29@ {

if(onTouchEnded)

@H_301_29@ onTouchEnded(touch,event);

@H_301_29@ };

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

return true;

@H_301_29@}

@H_301_29@

@H_301_29@怎么用呢很简单如下:

@H_301_29@

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

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


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

@H_301_29@ addChild(start);

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

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


exit->onTouchEnded = CC_CALLBACK_2(StartLayer::onBeginTouchExitButton,this);

@H_301_29@ addChild(exit);

@H_301_29@

@H_301_29@在定义onBeginTouchStartButton,和onBeginTouchExitButton

@H_301_29@


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

@H_301_29@{

@H_301_29@ StartGame(nullptr);

@H_301_29@}


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

@H_301_29@{

@H_301_29@ ExitGame);

@H_301_29@}

这样就ok啦,可别忘了在头文件添加其引用,到此结束,希望能帮到一些小忙。

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