Commit b1ab6400 authored by wangxin's avatar wangxin

碰撞

parent feed66e8
......@@ -1139,7 +1139,7 @@
},
"_contentSize": {
"__type__": "cc.Size",
"width": 46,
"width": -46,
"height": 245
},
"_anchorPoint": {
......@@ -1213,7 +1213,7 @@
"_enabled": true,
"_layoutSize": {
"__type__": "cc.Size",
"width": 46,
"width": -46,
"height": 245
},
"_resize": 1,
......@@ -1263,7 +1263,7 @@
},
"_contentSize": {
"__type__": "cc.Size",
"width": 46,
"width": 552,
"height": 245
},
"_anchorPoint": {
......@@ -1334,10 +1334,10 @@
"node": {
"__id__": 26
},
"_enabled": true,
"_enabled": false,
"_layoutSize": {
"__type__": "cc.Size",
"width": 46,
"width": 552,
"height": 245
},
"_resize": 1,
......
......@@ -105,10 +105,10 @@ cc.Class({
},
getDefaultData() {
const dataJson =
'{"pic_url":"http://staging-teach.cdn.ireadabc.com/ed94332a503c31e0908bd4c6923a2665.png","pic_url_2":"http://staging-teach.cdn.ireadabc.com/5fb60317ade0195d35ad8034d5370a7f.png","text":"This is a test label.","audio_url":"http://staging-teach.cdn.ireadabc.com/f47f1d7b5c160fe1c59500d180346240.mp3"}';
const data = JSON.parse(dataJson);
return data;
// const dataJson =
// '{"pic_url":"http://staging-teach.cdn.ireadabc.com/ed94332a503c31e0908bd4c6923a2665.png","pic_url_2":"http://staging-teach.cdn.ireadabc.com/5fb60317ade0195d35ad8034d5370a7f.png","text":"This is a test label.","audio_url":"http://staging-teach.cdn.ireadabc.com/f47f1d7b5c160fe1c59500d180346240.mp3"}';
// const data = JSON.parse(dataJson);
// return data;
},
preloadItem() {
......@@ -187,7 +187,13 @@ cc.Class({
op_p.getComponent("op_pic")._opId = i;
op_p.getComponent("op_pic").setPic(this.data.options[i].pic_url);
this.pic_container.addChild(op_p);
op_p.getComponent("op_pic")._ori_pos = cc.v2(op_p.x, op_p.y);
}
var self = this;
schduleOnce(function () {
self.pic_container.getComponent(cc.Layout).enabled = false;
}, 0.5);
},
initBg() {
......
......@@ -13,6 +13,16 @@ cc.Class({
},
_opId: 0, // 选项id
_ori_pos: null, // 初始位置
_isEnter: false, // 是否碰到上面的选项框
},
onLoad() {
this.nodePos = this.node.getPosition();
this.node.on("touchmove", this.onTouchMove, this);
this.node.on("touchend", this.onTouchEnd, this);
this.node.on("touchcancel", this.onTouchEnd, this);
},
setPic(url) {
......@@ -21,4 +31,75 @@ cc.Class({
this.pic.spriteFrame = spriteFrame;
});
},
//触摸移动;
onTouchMove(event) {
var self = this;
var touches = event.getTouches();
//触摸刚开始的位置
var oldPos = self.node.convertToNodeSpaceAR(touches[0].getStartLocation());
//触摸时不断变更的位置
var newPos = self.node.convertToNodeSpaceAR(touches[0].getLocation());
var subPos = oldPos.sub(newPos); // 2.X版本是 p1.sub(p2);
self.node.x = self.nodePos.x - subPos.x;
self.node.y = self.nodePos.y - subPos.y;
// 控制节点移不出屏幕;
var minX = -cc.view.getVisibleSize().width / 2 + self.node.width / 2; //最小X坐标;
var maxX = Math.abs(minX);
var minY = -cc.view.getVisibleSize().height / 2 + self.node.height / 2; //最小Y坐标;
var maxY = Math.abs(minY);
var nPos = self.node.getPosition(); //节点实时坐标;
if (nPos.x < minX) {
nPos.x = minX;
}
if (nPos.x > maxX) {
nPos.x = maxX;
}
if (nPos.y < minY) {
nPos.y = minY;
}
if (nPos.y > maxY) {
nPos.y = maxY;
}
self.node.setPosition(nPos);
},
onTouchEnd() {
this.nodePos = this.node.getPosition(); //获取触摸结束之后的node坐标;
if (this._isEnter) {
console.log("enter right");
} else {
console.log("enter right");
this.node.setPosition(this._ori_pos);
}
},
/**
* 当碰撞产生的时候调用
* @param {Collider} other 产生碰撞的另一个碰撞组件
* @param {Collider} self 产生碰撞的自身的碰撞组件
*/
onCollisionEnter: function (other, self) {
console.log("on collision");
if (other.getComponent("op_sound")._opId == this._opId) {
this._isEnter = true;
}
},
/**
* 当碰撞结束后调用
* @param {Collider} other 产生碰撞的另一个碰撞组件
* @param {Collider} self 产生碰撞的自身的碰撞组件
*/
onCollisionExit: function (other, self) {
console.log("on collision exit");
this._isEnter = false;
},
resetPos() {
// cc.tween(this.node).to()
},
});
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment