cocos2d js 给sprite增加按钮点击事件



1.首先定义监听事件

var touchListener_Sprite_touxiang_pic = cc.EventListener.create({//自己
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowtouches:true,
onTouchBegan: function(touch,event){

return true;//必须 return true
}.bind(this),
onTouchEnded: function(touch,event){
var target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(touch.getLocation());


if(locationInNode.x >= m_this.Pro_left_touxiang.getPositionX() &&
locationInNode.y >= m_this.Pro_left_touxiang.getPositionY() &&
locationInNode.x <= (m_this.Pro_left_touxiang.getPositionX()+130) &&
locationInNode.y <= (m_this.Pro_left_touxiang.getPositionY()+190))////// 判断之后执行的操作
{
m_this.spriteMsgUserLeftListen();
}

}
});


2.定义一个sprite,并将它和监听事件绑定,

this.Sprite_touxiang_pic_left = ccui.helper.seekWidgetByName(this.Pro_left_touxiang,"Sprite_touxiang_pic");
cc.eventManager.addListener(touchListener_Sprite_touxiang_pic,this);




具体可参考Samples里的 NewEventManagerTest

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...