如何使对象在鼠标按下时跳转到光标/触摸位置?
基于 createjs 拖放演示,我正在尝试制作它这样圆圈就会跳到鼠标按下时光标/触摸的位置。
我设法使拖动工作,但不知道如何使圆圈跳到位置。
createjs.Touch.enable(stage);
this.circle_mc.on("pressmove", function (evt) {
var point = stage.globalToLocal(evt.stageX, evt.stageY)
evt.currentTarget.x = point.x;
evt.currentTarget.y = point.y;
stage.update();
});
任何人都可以帮助它做这样的事情吗?
更新: 设法在动画中使用此代码使其正常工作:
var _this = this;
stage.on("stagemousedown", function (evt) {
var point = stage.globalToLocal(evt.stageX, evt.stageY)
_this.circle_mc.x = point.x;
_this.circle_mc.y = point.y;
var moveAround = stage.on("stagemousemove", function (evt) {
var point = stage.globalToLocal(evt.stageX, evt.stageY)
_this.circle_mc.x = point.x;
_this.circle_mc.y = point.y;
});
stage.on("stagemouseup", function (evt) {
stage.off("stagemousemove", moveAround)
}, null, true)
});
based on the createjs drag and drop demo, I'm trying to make it so that the circle jumps to the position of the cursor/touch on mousedown.
I managed to make the dragging work, but no idea how to make the circle jump to position.
createjs.Touch.enable(stage);
this.circle_mc.on("pressmove", function (evt) {
var point = stage.globalToLocal(evt.stageX, evt.stageY)
evt.currentTarget.x = point.x;
evt.currentTarget.y = point.y;
stage.update();
});
Can anyone help it do something like this?
Update: Managed to get it to work using this code in animate:
var _this = this;
stage.on("stagemousedown", function (evt) {
var point = stage.globalToLocal(evt.stageX, evt.stageY)
_this.circle_mc.x = point.x;
_this.circle_mc.y = point.y;
var moveAround = stage.on("stagemousemove", function (evt) {
var point = stage.globalToLocal(evt.stageX, evt.stageY)
_this.circle_mc.x = point.x;
_this.circle_mc.y = point.y;
});
stage.on("stagemouseup", function (evt) {
stage.off("stagemousemove", moveAround)
}, null, true)
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以轻松地做到这一点,但您必须重新进行拖动移动,因为默认情况下,它需要在对象上按下鼠标才能启动它。您还必须直接引用目标,而不是依赖事件目标。
这是一个带有更好评论的快速摆弄。
https://jsfiddle.net/lannymcnie/fn3gve74/1/
You can do this easily, but you have to re-make the dragmove, since by default it requires the mousedown on an object to kick it off. You also must reference the target directly instead of relying on an event target.
Here is a quick fiddle with better comments.
https://jsfiddle.net/lannymcnie/fn3gve74/1/