`
小嗷喏
  • 浏览: 51306 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

cocos2d-html5 Sprite 注册点击事件

    博客分类:
  • JS
阅读更多

   

      在 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 她真的很美。 

 

分享到:
评论

相关推荐

    cocos2d-x3D扩展3DToolKitforcocos2d-x.zip

    3D ToolKit for cocos2dx 是一个简单的cocos2d-x 3D扩展,可以方便地在cocos2dx环境中创建3D图形。 基于cocos2d-x 2.2开发。 已实现功能: 1,向量和矩阵运算。 2,扩展的3D相机。(支持 透视投影 和 ...

    cocos2d-x学习笔记(2)--addSprite.rar

    cocos2d-x学习笔记(2)--addSprite.rar

    cocos2d-x游戏开发详细教程, 附带超详细cocos2d学习路径图

    Cocos2D也拥有几个主要版本,包括Cocos2D-iPhone、Cocos2D-X,以及被社区普遍看好的Cocos2D-HTML5和JavaScript bindings for Cocos2D-X。同时也拥有了非常优秀的编辑器(独立编辑器),例如SpriteSheet Editors、...

    cocos2d-x游戏开发详细教程

    Cocos2D也拥有几个主要版本,包括Cocos2D-iPhone、Cocos2D-X,以及被社区普遍看好的Cocos2D-HTML5和JavaScript bindings for Cocos2D-X。同时也拥有了非常优秀的编辑器(独立编辑器),例如SpriteSheet Editors、...

    Rapid Game Development Using Cocos2d-JS(Apress,2016)

    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.by.Example.Beginners.Guide.2nd.Edition.1785288857

    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: ...

    Rapid.Game.Development.Using.Cocos2d-JS

    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-UIEditor

    使用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学习笔记(16)--spritesheet(精灵表单).rar

    Building Android Games with Cocos2d-x(PACKT,2015)

    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-x3.0来给Sprite添加遮罩

    cocos2d-x动画资源

    cocos2d-x的动画资源素材,在cocos2d-x中添加如下代码使用: //动画 //创建一个缓存 auto cache = SpriteFrameCache::getInstance(); //先把图片读入内存 cache->addSpriteFramesWithFile("anim.plist"); ...

    Cocos2D-x游戏开发之CCScale9Sprite

    Cocos2D-x游戏开发之CCScale9Sprite and CCControlButton

    AnimBear cocos2d-x

    本文实践自 Ray Wenderlich、Tony Dahbura 的文章《How to Use Animations and Sprite Sheets in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建一个...

    英文原版-Rapid Game Development Using Cocos2dJS 1st Edition

    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 转码显示

    cocos2d-x 3.X 接收图片 base64 转码显示,最终图片流显示在 Sprite 上

    cocos2dx 3.x实现精灵色相的修改

    Debug模式下,需要注释掉 CCGLProgramState.h apply() 方法中 switch语句default分支下的断言 调用方法 CSpriteWithHue *sprite = CCSpriteWithHue::create("HelloWorld....sprite->setHue(4.6);//值在 0 ~ 2 Pi 之间

    sample-cocos2d-html5-tiled-map-spritesheet

    Cocos2d-HTML5 2.2平铺的地图示例 有关许可证条款和条件,请参阅 。 此示例用于说明在Cocos2d-HTML5 2.2引擎和Intel XDK中平铺地图的用法。 这些资产和代码是从Jucimar Junior的创建的。 申请文件 资产/ lib / ...

    Building.Android.Games.with.Cocos2d-x.1785283839

    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...

    Cocos2d-x 3.x入门教程(一):基础概念

    前言 接触游戏开发时间不长,之前一直都是写Lua,写Lua肯定是没有什么发展的啦,至少你要会写的。写客户端,肯定要看看Cocos2d-x了。...在Cocos2d-x中,主要的概念包括Director、Scene、Layer和Sprite这

Global site tag (gtag.js) - Google Analytics