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