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

本文介绍如何在Cocos2d-x中创建一个自定义精灵类`CustomSprite`,使得精灵能响应并处理`EventTouch`事件。通过添加事件监听器,实现了`onTouchBegan`, `onTouchMoved`, `onTouchEnded`和`onTouchCancelled`的回调函数,用于在精灵被触摸时触发相应操作。示例代码展示了如何创建精灵并添加事件监听,以及如何定义回调函数处理触摸事件。" 6445221,136929,Windows API 源码解析:BaseInitializeContext 函数,"['c', 'winapi', '汇编', 'os']
摘要由CSDN通过智能技术生成

废话少说,上代码:

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

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; // 根据精灵帧名创建

    

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值