制作一个Javascript游戏,滚动有点问题

时间:2010-04-25 07:56:49

标签: javascript jquery

我有一个#wrapper div和一个#grid div嵌套在里面。目前我可以使用下面的这个功能滚动。

getCursorPos : function(){

    // set the empty cursor object
    var cursor = {};

    //get the offset from the left of the grid container
    var grid
    //offset loop
    $(function getCursorPos(){
        grid = $('#grid').offset();
        setTimeout(getCursorPos, game.loopSpeed);
    });

    //continuosly get the position
    var that = this;
    $(document).mousemove(function(e){

        //if game mode is menu exit
        if(game.mode === 'menu'){
            return;
        }

        // NOTE: this looks a litle over done but don't remove anything
        //       its like this because javascript uses floating points
        //       and so in order to line up to the nearest hunderedth I
        //       had to make the cursor and div position intergers by 
        //       muliplying by ten. one the two are added I reduced them
        //       and rounded them. 
        that.x = Math.round(((e.pageX * 10) - (grid.left * 10)) / 10);
        that.y = Math.round(((e.pageY * 10) - (grid.top * 10)) / 10);
    });

},

问题是鼠标坐标仅在鼠标移动时更新。有没有办法在不移动鼠标的情况下获得坐标?

1 个答案:

答案 0 :(得分:0)

您最后一次鼠标移动时始终拥有鼠标的最新最新坐标,请说明这些对您没用的原因。