在 cocos2d-html5, 发现 Sprite 类没有点击事件,需要注册点击的代理, 才能接受点击事件。下面一段代码写了一个可以接受点击事件的 DemoSprite 类。
var DemoSprite = cc.Sprite.extend({ _touchBegan: false, _touchEnabled: true, ctor: function (image) { this._super(); this.init(image); }, onEnter: function () { cc.Director.getInstance().getTouchDispatcher()._addTargetedDelegate(this, 0); // 当Sprite加载完后, 注册点击事件(有好几种注册的方式,在这里就不一一介绍了)。 this._touchEnabled = true; this._super(); }, onExit: function () { cc.Director.getInstance().getTouchDispatcher()._removeDelegate(this); //当Sprite退出后,取消点击事件的注册。 this._touchEnabled = false; this._super(); }, touchRect: function () { return this.getBoundingBoxToWorld(); }, onTouchBegan: function (touch, event) { if (cc.rectContainsPoint(this.touchRect(), touch.getLocation())) { //当点击在 Sprite 范围内时,执行。 //在这里处理点击事件。 this._touchBegan = true; return true; //返回true, 才会执行 onTouchEnded方法。 } return false; }, onTouchEnded: function (touch, event) { if (this._touchBegan) { this._touchBegan = false; } } });
ps: cocos2d-html5 她真的很美。
相关推荐
3D ToolKit for cocos2dx 是一个简单的cocos2d-x 3D扩展,可以方便地在cocos2dx环境中创建3D图形。 基于cocos2d-x 2.2开发。 已实现功能: 1,向量和矩阵运算。 2,扩展的3D相机。(支持 透视投影 和 ...
cocos2d-x学习笔记(2)--addSprite.rar
Cocos2D也拥有几个主要版本,包括Cocos2D-iPhone、Cocos2D-X,以及被社区普遍看好的Cocos2D-HTML5和JavaScript bindings for Cocos2D-X。同时也拥有了非常优秀的编辑器(独立编辑器),例如SpriteSheet Editors、...
Cocos2D也拥有几个主要版本,包括Cocos2D-iPhone、Cocos2D-X,以及被社区普遍看好的Cocos2D-HTML5和JavaScript bindings for Cocos2D-X。同时也拥有了非常优秀的编辑器(独立编辑器),例如SpriteSheet Editors、...
Get a gentle introduction to the Cocos2d-JS framework to begin working with sprite manipulations, animations, and other 2d game development topics. This book covers environment setup and getting ...
Title: Cocos2d-x by Example: Beginner’s Guide, 2nd Edition Author: Roger Engelbert Length: 250 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-04-30 ISBN-10: ...
Get a gentle introduction to the Cocos2d-JS framework to begin working with sprite manipulations, animations, and other 2d game development topics. This book covers environment setup and getting ...
使用cocos2d-x编写的ui编辑器(界面编辑器),实现了ui的可视化编辑,支持sprite、button、label、scale9sprite等等,导出配置文件格式为json格式-Cocos2d-x prepared ui interface editor, ui visual editing ...
cocos2d-x学习笔记(16)--spritesheet(精灵表单).rar
Cocos2d-x is a multi-platform C++ gaming framework in active development maintained by Chukong technologies. It wraps all the essential elements needed for creating a game, making the task of game ...
使用cocos2d-x3.0来给Sprite添加遮罩
cocos2d-x的动画资源素材,在cocos2d-x中添加如下代码使用: //动画 //创建一个缓存 auto cache = SpriteFrameCache::getInstance(); //先把图片读入内存 cache->addSpriteFramesWithFile("anim.plist"); ...
Cocos2d-x 本项目用到的功能 Sprite3D Animation3D Mesh Billboard Camera Light New audio engine 更多详情、使用方法,请下载后阅读README.md文件
Cocos2D-x游戏开发之CCScale9Sprite and CCControlButton
本文实践自 Ray Wenderlich、Tony Dahbura 的文章《How to Use Animations and Sprite Sheets in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建一个...
Get a gentle introduction to the Cocos2d-JS framework to begin working with sprite manipulations, animations, and other 2d game development topics. This book covers environment setup and getting ...
cocos2d-x 3.X 接收图片 base64 转码显示,最终图片流显示在 Sprite 上
Cocos2d-HTML5 2.2平铺的地图示例 有关许可证条款和条件,请参阅 。 此示例用于说明在Cocos2d-HTML5 2.2引擎和Intel XDK中平铺地图的用法。 这些资产和代码是从Jucimar Junior的创建的。 申请文件 资产/ lib / ...
Title: Building Android Games with Cocos2d-x Author: Raydelto Hernandez Length: 147 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-03-27 ISBN-10: 1785283839 ISBN...
前言 接触游戏开发时间不长,之前一直都是写Lua,写Lua肯定是没有什么发展的啦,至少你要会写的。写客户端,肯定要看看Cocos2d-x了。...在Cocos2d-x中,主要的概念包括Director、Scene、Layer和Sprite这