Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

//


#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.

}

:15px; font-family:Menlo; color:rgb(0,pass thru.

};

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

{

if(onTouchEnded)

onTouchEnded(touch,event);

};

@H_696_614@ 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));


@H_696_614@ 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->onTouchEnded = CC_CALLBACK_2(StartLayer::onBeginTouchExitButton,this);

addChild(exit);


在定义@H_389_772@onBeginTouchStartButton,和@H_389_772@onBeginTouchExitButton

@H_389_772@@H_389_772@


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

{

StartGame(nullptr);

}


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

{

ExitGame);

}

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

大佬总结

以上是大佬教程为你收集整理的cocos2dx3.2 谈谈精灵也能加入触摸事件回调函数简单使用,呢吗有木有更有的方法全部内容,希望文章能够帮你解决cocos2dx3.2 谈谈精灵也能加入触摸事件回调函数简单使用,呢吗有木有更有的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。