0
我正在尝试使用createjs画布在点击上的舞台上的空点上添加新状态(圆形),但在点击时拖动现有点。我假设问题是,当我尝试删除handleMove中的stagemousemove事件时,它没有正确删除,因为它没有专用的静态处理程序,但我不知道我怎么会创建该处理程序。创建形状/拖放和放下
// create new state (circle)
function createState(e) {
x = e.stageX
y = e.stageY
if (stage.children.every(el =>
!(el.hitTest(x, y)) &&
!(el.hitTest(x + 30, y + 30)) &&
!(el.hitTest(x + 30, y - 30)) &&
!(el.hitTest(x - 30, y + 30)) &&
!(el.hitTest(x - 30, y - 30))
)) {
state = new createjs.Shape()
state.graphics.ss(3)
.s('#777')
.f('#eaeaea')
.dc(x, y, 30)
stage.addChild(state)
state.on('mousedown', handleMove)
stage.update()
}
}
// drag and drop
function handleMove(e) {
el = stage.getObjectUnderPoint(stage.mouseX, stage.mouseY);
var offset = {
x: el.x - e.stageX,
y: el.y - e.stageY
}
stage.on('stagemousemove', (evt) => {
el.x = offset.x + evt.stageX
el.y = offset.y + evt.stageY
stage.update()
})
stage.on('stagemouseup', (e) => {
stage.off('stagemousemove', (evt) => {
el.x = offset.x + evt.stageX
el.y = offset.y + evt.stageY
stage.update()
})
})
}
// determines whether to create new point
function handler(e) {
if (stage.getObjectUnderPoint(stage.mouseX, stage.mouseY) == null)
createState(e)
}
stage.on('stagemousedown', handler)
谢谢Lanny,这工作完美。我不知道你可以从'on()'调用返回一个引用。尽管处理程序有一个问题。当我点击一个现有的状态(圆圈)并快速拖动时,它会沿着我拖动的方向创建一个新状态,而不是实际拖动圆圈。 –
听起来好像你的'handler()'每次都在触发。请注意,无论点击什么,“stagemousedown”每次都会触发。另一件事 - 你看看“pressmove”事件处理程序?对拖放操作进行很多监听器管理。 – Lanny
我已经看过''pressmove'',但是在计算没有两个处理程序时拖动的偏移量时遇到了问题。我明白每次都会触发'stagemousedown',但是如果点击目标是一个状态,我的'if'语句不应该创建新的状态吗?看起来有点拖延时,点击并拖动快速。 [在这里看到一个演示](http://www.greg-wolff.com/fasdemo)。请注意,如果您快速点击并拖动某个状态,它会创建一个新状态。在控制台中记录鼠标目标。 –