cocos 2d CCSprite 触摸识别 非常有用!!!!!
cocos 2d 中的CCSprite 无法识别触摸操作,需要自定义类。
解决思想:找到触摸发生的那个点,判断其是否在sprite的矩形区域内
完整代码如下:
//MySprite.h文件 #import "CCSprite.h" #import "cocos2d.h"//自定义类继承与CCSprite,实现CCTouchOneByDelegate协议 @interface MySprite : CCSprite<CCTouchOneByOneDelegate> @property (nonatomic,copy) NSString* spriteName; //定义精灵名属性 -(id)initWithName:(NSString*)spriteNameT; @end//MySprite.m文件 #import "MySprite.h"@implementation MySprite @synthesize spriteName;-(id)initWithName:(NSString *)spriteNameT {if (self = [super init]) {spriteName = spriteNameT;}return self; }-(void)onEnter {//注册触摸控制[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];[super onEnter]; }-(void)onExit {//取消触摸控制 [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];[super onExit]; }//get the rect of current sprite -(CGRect)spriteRect {//计算精灵的矩形区域并返回return CGRectMake( _position.x - _contentSize.width*_anchorPoint.x,_position.y - _contentSize.height*_anchorPoint.y,_contentSize.width, _contentSize.height); } //判断触摸点是否在精灵矩形区域内 -(BOOL)isContainsTouchPoint:(UITouch*)touchT {CCLOG(@"sprite touch event ______________________________________________");CGRect spriteRect = [self spriteRect];spriteRect.origin = CGPointZero;CGPoint touchPointInView = [touchT locationInView:[touchT view]];NSLog(@"touch point in touch view is : %f,%f",touchPointInView.x,touchPointInView.y);//转换为GL坐标系touchPointInView = [[CCDirector sharedDirector] convertToGL:touchPointInView];CGPoint touchPoint = [self convertToNodeSpace:touchPointInView];BOOL isTouched = CGRectContainsPoint(spriteRect, touchPoint);NSLog(@"%@",isTouched? @"YES" : @"NO");if (isTouched) {// CCLOG(@"sprite is touched here");}else{NSLog(@"sprite is not touched here");}return isTouched; }//实现触摸协议 #pragma mark touch one by one delegate -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {return [self isContainsTouchPoint:touch]; } -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {NSLog(@"sprite name is %@",spriteName); } @end
这个精灵是可以背捕捉到的。
转载于:https://www.cnblogs.com/helmsyy/p/3614383.html
cocos 2d CCSprite 触摸识别 非常有用!!!!!相关推荐
- 触摸识别,智能分拣……看AI如何为垃圾分类赋能
点击上方"民工哥技术之路"选择"星标" 每天10点为你分享不一样的干货 读者福利!多达 2048G 各种资源免费赠送 文章转载自李开复老师的公众号 垃圾分类是 ...
- 【触觉AI】麻省理工研发带550个传感器的触觉手套,通过触摸识别物体|湾区人工智能...
点击上方蓝色字体,关注我们 麻省理工学院计算机科学与人工智能实验室(MIT-CSAIL)的研究人员近日开发了一种低成本的传感器手套,旨在使人工智能能够"弄清楚"人类如何通过触摸识别 ...
- deepstream 2D 3D 动作识别(包括file、rtsp输入输出)
deepstream 2D 3D 动作识别(包括file.rtsp输入输出) deepstream c/c++ 在nvidia官方deepstream-3d-action-recognition的基础 ...
- 【cocos 2d微信小游戏开发教程】基础使用笔记分享(三)
富文本(RichText) 优点:自定义颜色,大小,描边,还能加图片.对于复杂的文本表现力更好. 缺点:cocos的富文本是由Label组件拼装实现的.低版本会打断合批.Label太多导致卡顿. 常用 ...
- Cocos中的触摸机制总结(附相关C++11基础)
Cocos2d-x学习之路(个人总结) http://www.docin.com/p-1489599279.html 四.触摸篇 1.传统的单点触摸(针对scene的触屏事件) 1.1单点触摸的声明 ...
- cocos 2d - js
cocos2d - js 知识整理 目录结构 index.html main.js代码 project.json文件 启动流程 导演 节点 节点常用基本属性 节点坐标 场景 图层Layer 精灵Spr ...
- cocos2d-x游戏引擎核心(3.x)----启动渲染流程
(1) 首先,这里以win32平台下为例子.win32下游戏的启动都是从win32目录下main文件开始的,即是游戏的入口函数,如下: #include "main.h" #inc ...
- 一文为你详解2D与3D人脸识别有什么区别?
最近业界内刮起了一股"人脸识别安全"的大讨论,小到个人大到超市以及银行,都在使用这个刷脸认证或支付,说它好吧,确实解决了无接触,快速高效等问题,你说它不好吧,也是有原因的,比如最明 ...
- 2D与3D人脸识别有什么本质上的区别?
https://www.zhihu.com/question/324123433/answer/681365180 https://www.zhihu.com/question/324123433/a ...
最新文章
- lightgbm保存模型参数
- 零基础学习Java培训有什么攻略
- 2020年中国面向人工智能“新基建”的知识图谱行业白皮书
- KVM为虚拟机添加设备总结
- 文巾解题 231. 2的幂
- Linux日常命令使用记录
- centos下python中添加easygui模块
- mysql免安装_腾讯云Ubuntu18.04部置Django2系列(二):Ubuntu18.04 安装Mysql
- 飞步神速!何晓飞团队完成无人车深度学习芯片流片,算力创国内新高
- Oracle ORA-08591告警
- [推荐书籍]Flex 4 in Action MEAP Edition
- 联想拯救者y7000p加内存条_内存、硬盘不够用?手把手教你升级联想拯救者Y7000P...
- 西门子g120变频器接线图_西门子1500PLC通过工艺对象对G120变频器组态和调试
- 《爱的五种能力》阅读笔记(完整版)
- linux twiki 添加权限,Ubuntu上TWiki安装和使用心得
- 流量卡之家:5G从概念到落地,未来工厂触手可及
- 30亿流量!梨视频靠什么拍出个“短视频世界杯元年”
- js表格隔行变色案例
- DB2 V9.7新特性 - 降低高水位标记
- GSM MODEM的基本用法-收发短信篇