Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dragging Objects

    var mousedown = false;
    var savedSettings;
    var capturedBall = null;
    document.onmousedown = function(evt){
    mousedown = true;
    var mouseOffsetX = evt.pageX - canvas.offsetLeft;
    var mouseOffsetY = evt.pageY - canvas.offsetTop;
    for(var i= 0; i<balls.length; i++){
    var tmp = balls[i].getBounds();
    if(mouseOffsetX>tmp.x&&mouseOffsetX<(tmp.x+tmp.width)
    && mouseOffsetY>tmp.y&&mouseOffsetY<(tmp.y+tmp.height))
    {
    savedSettings = {vx:balls[i].vx,vy:balls[i].vy};
    balls[i].vx = 0;
    balls[i].vy = 0;
    balls[i].x = mouseOffsetX;
    balls[i].y = mouseOffsetY;
    capturedBall = balls[i];
    break;
    }
    }
    };
    document.onmousemove = function(evt){
    if(mousedown){
    capturedBall.x = evt.pageX;
    capturedBall.y = evt.pageY;
    }
    };
    document.onmouseup = function(evt){
    if(mousedown){
    capturedBall.vx = savedSettings.vx;
    capturedBall.vy = savedSettings.vy;
    mousedown = false;
    }
    } ;

  • #2
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    //better spacing

    var mousedown = false;
    var savedSettings;
    var capturedBall = null;

    document.onmousedown = function(evt){
    mousedown = true;
    var mouseOffsetX = evt.pageX - canvas.offsetLeft;
    var mouseOffsetY = evt.pageY - canvas.offsetTop;

    for(var i= 0; i<balls.length; i++){
    var tmp = balls[i].getBounds();

    if(mouseOffsetX>tmp.x&&mouseOffsetX<(tmp.x+tmp.width)
    && mouseOffsetY>tmp.y&&mouseOffsetY<(tmp.y+tmp.height))
    {

    savedSettings = {vx:balls[i].vx,vy:balls[i].vy};
    balls[i].vx = 0;
    balls[i].vy = 0;
    balls[i].x = mouseOffsetX;
    balls[i].y = mouseOffsetY;
    capturedBall = balls[i];
    break;
    }
    }
    };

    document.onmousemove = function(evt){
    if(mousedown){
    capturedBall.x = evt.pageX;
    capturedBall.y = evt.pageY;
    }
    };

    document.onmouseup = function(evt){
    if(mousedown){
    capturedBall.vx = savedSettings.vx;
    capturedBall.vy = savedSettings.vy;
    mousedown = false;

    }
    } ;

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Enclose your code in [code] [/code] to preserve formatting.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,084
    Thanks
    38
    Thanked 498 Times in 492 Posts
    I thought this forum section was for completed scripts.

    Where is the 'balls' array defined?
    Difficult to tell if it is a useful snippet or not without an example of its' use.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •