简体   繁体   English

在gamequery中,我试图通过使用mousetracker单击对象并将其拖动来移动所选对象

[英]in gamequery I am trying to move a selected object with mousetracker by clicking on the object, and dragging it

I know I can use mousedown selection for it, but I am wanting the clicked on sprite to follow my mouse, there is a mousetracker function of sorts mentioned in the api; 我知道我可以为此使用mousedown选择,但是我希望单击的精灵跟随我的鼠标,该api中提到了各种mousetracker功能。 but unfortunately there are no examples of this other than stating that it allows mouse detection. 但不幸的是,除了声明它允许鼠标检测之外,没有其他示例。

//add mousedown events for yarnballs.
$(".gQ_sprite").mousedown(function() {
    clickedDivId = $(this).attr('id');
    if(clickedDivId.charAt(8) == "-")
    {
        currentClickedDivId = clickedDivId
        $(document).mousemove(function(e){
            spriteXPosition = e.pageX
            spriteYPosition = e.pageY
        });

    }
});

I have the location of the mouse selected, just not sure how to get the selected sprite to follow it. 我已经选择了鼠标的位置,只是不确定如何使所选的精灵跟随它。

any help would be greatly appreciated. 任何帮助将不胜感激。

What Mati said is correct: $.gQ.mouseTracker allows you to access the mouse's state outside of an event handler. 马蒂说的是正确的: $ .gQ.mouseTracker允许您在事件处理程序之外访问鼠标的状态。 The example he gives is correct but it can't be used to move a gQ object (sprite, tile-map or group) around because you'r not allowed to use the .css() function for those. 他给出的示例是正确的,但不能用于移动gQ对象(精灵,平铺图或组),因为不允许您使用.css()函数。 Doing so will break collision detection. 这样做会破坏碰撞检测。

If what you want is to move a gQ object you should do this instead : 如果要移动gQ对象,则应改为:

$('#' + currentClickedDivId).xy($.gQ.mouseTracker.x, $.gQ.mouseTracker.y);

But since this should be done in a periodical callback, the smoothness of the dragging will depend on the refresh rate. 但是,由于应在定期回调中完成此操作,因此拖动的平滑度将取决于刷新率。

If you want to use event handlers instead you could modify you code to look like this (without using the mouseTracker): 如果要使用事件处理程序,则可以将代码修改为如下所示(不使用mouseTracker):

var clickedDiv;
var clickedDivOffset = {x:0, y:0};

$(".gQ_sprite").mousedown(function(e) {
    clickedDiv = $(this);
    clickedDivOffset = {
        x: e.pageX - clickedDiv.x() - $().playground().offset().left,
        y: e.pageY - clickedDiv.y() - $().playground().offset().top
    };
});

$(".gQ_sprite").mouseup(function() {
    clickedDiv = false;
});

$().playground().mousemove(function(e) {
    if(clickedDiv){
        clickedDiv.xy(
            e.pageX - clickedDivOffset.x,
            e.pageY - clickedDivOffset.y,
        );
    }
});

This will implement a drag-n-drop effect. 这将实现拖放效果。 If you want the clicked element to stick to the mouse you will have to slightly adapt the code but the basics will remain the same. 如果您希望单击的元素贴在鼠标上,则必须稍微修改一下代码,但基本知识将保持不变。

According to the documentation : 根据文档

If the mouse tracker is enabled you can check the state of the mouse at anytime by looking into the object $.gQ.mouseTracker where x and y contain the position of the mouse and 1, 2 and 3 a boolean value where true means that the first, second or thrid button is pressed. 如果启用了鼠标跟踪器,则可以随时通过查看对象$ .gQ.mouseTracker来检查鼠标的状态,其中x和y包含鼠标的位置,而1、2和3是布尔值,其中true表示按下第一,第二或第三按钮。

Observe the output of: 观察以下内容的输出:

$("#playground").playground({ refreshRate: 60, mouseTracker: true });

$.playground().startGame();
$.playground().registerCallback(function(){
    console.log( $.gQ.mouseTracker );
}, 1000);

To make those divs actually follow the cursor, you have to use .css() 为了使这些div实际上跟随光标,您必须使用.css()

$('#' + currentClickedDivId).css({
    top:  $.gQ.mouseTracker.y + 'px',
    left: $.gQ.mouseTracker.x + 'px'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM