用的cocos creator惯疙。最近本人寫麻將拖拽出牌時需用到手勢钱床。自己簡單的了解了下荚斯。(本人新手)。實(shí)現(xiàn)效果查牌。拖拽一個組件到界面任意位置事期,組件隨手指移動。
cc.Class({
extends: cc.Component,
properties: {
label: {
default: null,
type: cc.Label
},
// defaults, set visually when attaching this script to the Canvas
text: 'Hello, World!',
mySpri:{
default:null,
type:cc.Sprite,
},
myButton:{
default:null,
type:cc.Button,
},
},
// use this for initialization
onLoad: function () {
this.label.string = this.text;
this.addMJTouch(this.mySpri,this.mySpri.node.getPosition());
this.addMJTouch(this.myButton,this.myButton.node.getPosition());
},
//添加觸摸手勢方法
addMJTouch:function(touchMJSpri,MJOriPosition){
var self=this;
touchMJSpri.node.on(cc.Node.EventType.TOUCH_START,function(event){//開始觸摸纸颜,也可以只寫這一個兽泣。
var temp = event.getLocation();
var tempPlayer = touchMJSpri.node.parent.convertToNodeSpaceAR(temp)
touchMJSpri.node.setPosition(tempPlayer);
})
touchMJSpri.node.on(cc.Node.EventType.TOUCH_MOVE,function(event){//移動中
var temp = event.getLocation();
var tempPlayer = touchMJSpri.node.parent.convertToNodeSpaceAR(temp)
touchMJSpri.node.setPosition(tempPlayer) ;
})
touchMJSpri.node.on(cc.Node.EventType.TOUCH_END,function(event){//手指結(jié)束觸摸
var temp = event.getLocation();//全局坐標(biāo)
var tempPlayer= touchMJSpri.node.parent.convertToNodeSpaceAR(temp);//轉(zhuǎn)化為局部坐標(biāo)
if(touchMJSpri==self.mySpri){
touchMJSpri.node.setPosition(tempPlayer) ;//組件最終停留在手指終止的地方
}else{
touchMJSpri.node.setPosition(MJOriPosition) ;//組件停留在組件最初所在位置
}
})
},
// called every frame
update: function (dt) {
},
});