Commit 172bc42b authored by Friends233's avatar Friends233

拖拽填入方块

parent 3086c8db
This diff is collapsed.
...@@ -31,13 +31,12 @@ export default class Block extends cc.Component { ...@@ -31,13 +31,12 @@ export default class Block extends cc.Component {
/** 放置block */ /** 放置block */
setBlockMatrix() { setBlockMatrix() {
const targetNodeWorldPos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition())
const matrix = cc.find('blockMatrix', this.node.parent.parent) const matrix = cc.find('blockMatrix', this.node.parent.parent)
const pos = this.node.convertToWorldSpaceAR(this.node.getPosition()) const { targetPos, targetNode } = this.posFindBlock(targetNodeWorldPos)
const { targetPos, targetNode } = this.posFindBlock(pos)
this.isMove = false this.isMove = false
const p = targetNode.convertToNodeSpaceAR(cc.v2(targetPos.x, targetPos.y)) const p = this.node.convertToNodeSpaceAR(targetPos)
console.log('set',p.x,p.y) this.node.setPosition(cc.v2(p.x + this.node.x, p.y + this.node.y))
this.node.setPosition(p)
} }
/** /**
...@@ -46,22 +45,21 @@ export default class Block extends cc.Component { ...@@ -46,22 +45,21 @@ export default class Block extends cc.Component {
*/ */
posFindBlock(pos: cc.Vec2) { posFindBlock(pos: cc.Vec2) {
const matrix = cc.find('blockMatrix', this.node.parent.parent) const matrix = cc.find('blockMatrix', this.node.parent.parent)
const matrixPos = matrix.children.map((node) => {
const nodePos = node.convertToWorldSpaceAR(node.getPosition())
return {
x: nodePos.x,
y: nodePos.y + 300
}
})
let minX = 999999, minIdx = -1 let minX = 999999, minIdx = -1
matrixPos.forEach((nodePos: cc.Vec2, i) => { const matrixPos = matrix.children.map((node, i) => {
const nodePos = matrix.convertToWorldSpaceAR(node.getPosition())
// 两点间的距离
const offsetX = Math.sqrt(Math.pow((nodePos.x - pos.x), 2) + Math.pow((nodePos.y - pos.y), 2)) const offsetX = Math.sqrt(Math.pow((nodePos.x - pos.x), 2) + Math.pow((nodePos.y - pos.y), 2))
if (offsetX < minX) { if (offsetX < minX) {
minX = offsetX minX = offsetX
minIdx = i minIdx = i
} }
return {
nodePos,
node
}
}) })
return { targetPos: matrixPos[minIdx], targetNode: matrix.children[minIdx] } return { targetPos: matrixPos[minIdx].nodePos, targetNode: matrixPos[minIdx].node }
} }
addNodeEvent() { addNodeEvent() {
......
...@@ -19,47 +19,38 @@ export default class NewClass extends cc.Component { ...@@ -19,47 +19,38 @@ export default class NewClass extends cc.Component {
/** 方块矩阵外层容器 */ /** 方块矩阵外层容器 */
blockMatrix: cc.Node = null blockMatrix: cc.Node = null
/** 外层容器 */
stage: cc.Canvas = null
start() { start() {
this.viewHeight = cc.view.getVisibleSize().height this.viewHeight = cc.view.getVisibleSize().height
this.viewWidth = cc.view.getVisibleSize().width this.viewWidth = cc.view.getVisibleSize().width
this.blockMatrix = cc.find('blockMatrix', this.node.parent)
this.stage = this.node.parent
this.addNodeEvent() this.addNodeEvent()
const matrix = this.blockMatrix = cc.find('blockMatrix', this.node.parent)
const node = matrix.children[0]
const k = node.convertToWorldSpaceAR(node.getPosition())
console.log('start', k.x, k.y)
} }
setBlockMatrixAll(parentPos: cc.Vec2) { setBlockMatrixAll() {
this.isMove = false this.isMove = false
const allBlock = this.node.children const allBlock = this.node.children
allBlock.forEach((node: cc.Node) => { allBlock.forEach((node: cc.Node) => {
const scripts = node.getComponent('block')
scripts.setBlockMatrix()
}) })
const node = allBlock[0]
const scripts = node.getComponent('block')
// scripts.setBlockMatrix()
} }
addNodeEvent() { addNodeEvent() {
this.node.on(cc.Node.EventType.TOUCH_START, () => { this.node.on(cc.Node.EventType.TOUCH_START, () => {
console.log('TOUCH_START')
this.isMove = true this.isMove = true
this.node.scale = 1.57 this.node.scale = 1.7
}, this) }, this)
this.node.on(cc.Node.EventType.TOUCH_END, () => { this.node.on(cc.Node.EventType.TOUCH_END, () => {
console.log('TOUCH_END') this.setBlockMatrixAll()
this.setBlockMatrixAll(this.node.getPosition())
}, this) }, this)
this.node.on(cc.Node.EventType.TOUCH_MOVE, (e: cc.Event.EventTouch) => { this.node.on(cc.Node.EventType.TOUCH_MOVE, (e: cc.Event.EventTouch) => {
const allBlock = this.node.children
const matrix = this.blockMatrix
const node = allBlock[0]
const blockPos = node.convertToWorldSpaceAR(node.getPosition())
console.log(blockPos.x, blockPos.y);
console.log("========================")
if (this.isMove) { if (this.isMove) {
const pos: cc.Vec2 = e.getPreviousLocation() const pos: cc.Vec2 = e.getPreviousLocation()
......
...@@ -21,13 +21,10 @@ ...@@ -21,13 +21,10 @@
"_components": [ "_components": [
{ {
"__id__": 2 "__id__": 2
},
{
"__id__": 3
} }
], ],
"_prefab": { "_prefab": {
"__id__": 4 "__id__": 3
}, },
"_opacity": 255, "_opacity": 255,
"_color": { "_color": {
...@@ -108,16 +105,6 @@ ...@@ -108,16 +105,6 @@
"_atlas": null, "_atlas": null,
"_id": "" "_id": ""
}, },
{
"__type__": "b1207cbMWhNVLKTFVfAq/t2",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"_id": ""
},
{ {
"__type__": "cc.PrefabInfo", "__type__": "cc.PrefabInfo",
"root": { "root": {
......
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