如何使对象在鼠标按下时跳转到光标/触摸位置?

发布于 2025-01-14 13:51:41 字数 1200 浏览 0 评论 0原文

基于 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?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2025-01-21 13:51:41

您可以轻松地做到这一点,但您必须重新进行拖动移动,因为默认情况下,它需要在对象上按下鼠标才能启动它。您还必须直接引用目标,而不是依赖事件目标。

stage.on("stagemousedown", function(e){
    s.x = e.stageX; s.y = e.stageY;
  
  // Handle move
  var evt = stage.on("stagemousemove", function(e) {
    // Set to mouse position each move
    s.x = e.stageX; s.y = e.stageY;
  });
  
  // Handle release
  stage.on("stagemouseup", function(e) {
    stage.off("stagemousemove", evt)
  }, null, true)
});

这是一个带有更好评论的快速摆弄。
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.

stage.on("stagemousedown", function(e){
    s.x = e.stageX; s.y = e.stageY;
  
  // Handle move
  var evt = stage.on("stagemousemove", function(e) {
    // Set to mouse position each move
    s.x = e.stageX; s.y = e.stageY;
  });
  
  // Handle release
  stage.on("stagemouseup", function(e) {
    stage.off("stagemousemove", evt)
  }, null, true)
});

Here is a quick fiddle with better comments.
https://jsfiddle.net/lannymcnie/fn3gve74/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文