Making a Javascript game, Having a little problem with scrolling.

Posted by RobertWHurst on Stack Overflow See other posts from Stack Overflow or by RobertWHurst
Published on 2010-04-25T07:56:48Z Indexed on 2010/04/25 8:03 UTC
Read the original article Hit count: 262

I have a #wrapper div and a #grid div nested inside. currently I can scroll around with this function below.

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);
    });

},

the problem is that the mouse coordinates only update when the mouse moves. is there any way to get the coordinates with out moving the mouse?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about game-development