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

前端之家收集整理的这篇文章主要介绍了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

猜你在找的Cocos2d-x相关文章