在我点游戏界面中放置两个按钮: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.
}
};
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->ontouchended = CC_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);
}
这样就ok啦,可别忘了在头文件添加其引用,到此结束,希望能帮到一些小忙。 总结以上是内存溢出为你收集整理的cocos2dx3.2 谈谈精灵也能加入触摸事件回调函数简单使用,呢吗有木有更有的方法全部内容,希望文章能够帮你解决cocos2dx3.2 谈谈精灵也能加入触摸事件回调函数简单使用,呢吗有木有更有的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)