cocos2d-html5学习笔记(四)--键盘事件和touch事件

本文介绍如何在Cocos2d中实现用户输入处理,包括键盘和触摸事件的监听和响应。通过示例展示了如何为Layer及Sprite等节点启用触摸和键盘事件,并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

游戏是互动的,获取用户输入至关重要。cocos2d中目前只有Layer以及其子类默认能够获取用户输入,即触发用户输入的事件。其他节点需要开启触摸事件的话,需要自己手动实现。

现在看一个例子:

[javascript] view plain copy
  1. var Green=cc.Layer.extend({  
  2.       
  3.     init:function  () {  
  4.         var layer1=cc.LayerColor.create(cc.c4(0,255,0,255),320,480);  
  5.   
  6.         this.addChild(layer1);  
  7.         this.setKeyboardEnabled(true);  
  8.         this.setTouchEnabled(true);  
  9.   
  10.         return true;  
  11.     },  
  12.   
  13.     onKeyUp:function (key) {  
  14.         window.alert(key);  
  15.     },  
  16.   
  17.     onKeyDown:function (key) {  
  18.         window.alert(key);  
  19.     },  
  20.   
  21.     onTouchesEnded:function (touches,event) {  
  22.         alert(touches[0].locationInView().x);  
  23.     }  
  24. });  

在绿色层左边点击一下,效果如图:

至于可以有哪些事件,自己可以查看API文档的cc.KeypadDelegate和cc.StandardTouchDelegate。

至于其他节点,比如Sprite,需要手动实现,主要用到的方法是:cc.Director.getInstance().getTouchDispatcher().addStandardDelegate()和cc.Director.getInstance().getTouchDispatcher().removeDelegate。一下是我的一个实现。

[javascript] view plain copy
  1. var CanTouchSprite=cc.Sprite.extend({  
  2.     _touchBegan:false,  
  3.     _touchEnabled:true,  
  4.     ctor:function(){  
  5.         this._super();  
  6.     },  
  7.     onEnter:function(){  
  8.         cc.Director.getInstance().getTouchDispatcher().addStandardDelegate(this, 0);  
  9.         this._touchEnabled=true;  
  10.         this._super();  
  11.     },  
  12.     onExit:function(){  
  13.         cc.Director.getInstance().getTouchDispatcher().removeDelegate(this);  
  14.         this._touchEnabled=false;  
  15.         this._super();  
  16.     },  
  17.     touchRect:function(){  
  18.         return this.getBoundingBoxToWorld();  
  19.     },  
  20.     setTouchEnabled:function(enable){  
  21.         if(enable&&!this._touchEnabled){  
  22.             cc.Director.getInstance().getTouchDispatcher().addStandardDelegate(this, 0);  
  23.             this._touchEnabled=true;  
  24.         }  
  25.         else if(!enable&&this._touchEnabled){  
  26.             cc.Director.getInstance().getTouchDispatcher().removeDelegate(this);  
  27.             this._touchEnabled=false;  
  28.         }  
  29.     },  
  30.     onTouchesBegan:function(touches,event){  
  31.         if(cc.Rect.CCRectContainsPoint(this.touchRect(),touches[0].getLocation())){  
  32.             this._touchBegan=true;  
  33.         }  
  34.     },  
  35.     onTouchesMoved:function(touches,event){  
  36.   
  37.     },  
  38.     onTouchesEnded:function(touches,event){  
  39.         if(this._touchBegan){  
  40.             this._touchBegan=false;  
  41.         }  
  42.     }  
  43. }) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值