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

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

概述废话少说,上代码: 其实上次也说过的只不过这次是完善啦一些。 CustomSprite.h头文件声明: // //  CustomSprite.h //  DontSaveMe // //  Created by Mr Gan on 12/23/14. // // #ifndef __DontSaveMe__CustomSprite__ #define __DontSaveMe__CustomSp

废话少说,上代码:

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

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)

log("StartLayer::onStart");

auto scene =MainScene::createScene();

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


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

总结

以上是内存溢出为你收集整理的cocos2dx3.2 自定义精灵让精灵能够回调接收EventTouch事件,全部内容,希望文章能够帮你解决cocos2dx3.2 自定义精灵让精灵能够回调接收EventTouch事件,所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1024984.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存