绑定鼠标点击事件
在cocoscreator中,ui坐标与世界坐标是不同的,所以要实现物体跟随移动就需要转换一下坐标
properties: {
weapon:cc.Node,
},
onLoad(){
//绑定事件
this.node.on(cc.Node.EventType.TOUCH_MOVE,this.mouseFun,this);
},
mouseFun(event){
console.log('鼠标移动了');
//触摸点的世界坐标
var pos=new cc.Vec2(event.getLocationX(),event.getLocationY());
//转换为UI坐标
pos=this.node.convertToNodeSpaceAR(pos);
//给要移动的物体赋值
this.weapon.position=pos;
//只移动x轴,Y轴同理
//this.weapon.x=pos.x;
},