cocos2dx3.2 自定义精灵让精灵能够回调接收EventTouch事件,

前端之家收集整理的这篇文章主要介绍了cocos2dx3.2 自定义精灵让精灵能够回调接收EventTouch事件,前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

废话少说,上代码

其实上次也说过的只不过这次是完善啦一些。

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:

CustomSprite();

virtual ~CustomSprite();

static CustomSprite* createWithPath(conststd::string &path);

static CustomSprite* createWithFrameName(conststd::string &name);


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:

void addEventListener();

virtual bool initWithFile(conststd::string& filename) override; //根据文件名创建

virtual bool initWithSpriteFrameName(conststd::string &name) override; // 根据精灵帧名创建

};


#endif /* defined(__DontSaveMe__CustomSprite__) */

CustomSprite.cpp实现文件如下:

//

// CustomSprite.cpp

// DontSaveMe

//

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

//


#include "CustomSprite.h"

CustomSprite::CustomSprite()

:onTouchBegan(nullptr)

,onTouchMoved(nullptr)

nullptr)

nullptr)

{

addEventListener();

}

CustomSprite::~CustomSprite()

{

Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);

}



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

auto sprite = new CustomSprite();

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

{

sprite->autorelease();

return sprite;

}

else

CC_SAFE_DELETE(sprite);

return nullptr;

CustomSprite* CustomSprite::createWithFrameName(const std::string &name)

if(sprite && sprite->initWithSpriteFrameName(name))

bool CustomSprite::initWithSpriteFrameName(const std::string &name)

if(!Sprite::initWithSpriteFrameName(name))

{

return false;

return true;

void CustomSprite::addEventListener()

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

listener->setSwallowTouches(true);

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

cocos2d::Vec2 p;

auto parent = this->getParent();

if (parent) {

p = parent->convertTouchToNodeSpace(touch); //转化为统一坐标系下比较

}

else

{

p = touch->getLocation(); //OpenGL的坐标

}


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

if(rect.containsPoint(p))

{

if(onTouchBegan)

onTouchBegan(touch,event);

returntrue; // to indicate that we have consumed it.

}

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

};

listener->onTouchEnded = [=](cocos2d::Touch* touch,27); margin-top:0px; margin-bottom:0px; font-size:15px; font-family:Menlo"> if(onTouchEnded)

onTouchEnded(touch,event);

listener->onTouchCancelled = [=](cocos2d::Touch* touch,27); margin-top:0px; margin-bottom:0px; font-size:15px; font-family:Menlo"> if(onTouchCancelled)

onTouchCancelled(touch,27); margin-top:0px; margin-bottom:0px; font-size:15px; font-family:Menlo"> listener->onTouchMoved = [=](cocos2d::Touch* touch,27); margin-top:0px; margin-bottom:0px; font-size:15px; font-family:Menlo"> if(onTouchMoved)

onTouchMoved(touch,27); margin-top:0px; margin-bottom:0px; font-size:15px; font-family:Menlo"> cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);

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

if(!Sprite::initWithFile(filename))

}

好了,下面讲讲如何用。首先添加文件CustomSprite.h这个是不用说的,呵呵。

然后创建精灵,添加精灵事件监听,如下:

auto start = CustomSprite::createWithFrameName("start1.png");

start->setPosition(Vec2(480,200));

start->onTouchEnded =CC_CALLBACK_2(StartScene::onStart,this);

addChild(start);

onStart回调代码如下:

void StartScene::onStart(Touch *touch,Event *event)

@H_301_544@log("StartLayer::onStart");

@H_301_544@auto scene =MainScene::createScene();

@H_301_544@Director::getInstance()->replaceScene(scene);


到这就结束了,用起来还是挺方便到吧

原文链接:https://www.f2er.com/cocos2dx/345321.html

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