Sunday 15 May 2011

javascript - Using addEventListener to add a callback with arguments -


I am creating drag and drop systems using a canvas.

  canvas.addEventListener ('moushudown', function () {window.initialClickX = mouse.x; window.initialClickY = mouse; y; window.initialBallX = ball.x; window.initialBallY = Ball; how; canvas.AdventListener ('mausame', onmosamov, false);}, false); Function On MouseShow () {ball.x = mouse.x + window.initialBallX - window.initialClickX; Ball.y = Mouse. Y + window.initialBallY - window.initialClickY; Attract (); }   

When I click, then I need to store the values ​​for the initial mouse position and initial ball position, so I can pull the ball right around it I am

The above code works perfectly, but I think it looks messy with all the global variables. I would like to be able to launch the initial parameters, initial click, initial ball and initial Ball But how can I add these parameters to the callback function?

Or if there is a better way of doing this, please let me know, thank you.

Try to use the cover function to do this.

  Canvas AddEventListener ('Masudown', function) {var initialClick x = mouse.x; Var initial click = mouse Y; StartitialBallX = ball.x; Var initialBallY = ball.y; Canvas.addEventListener ('mausomov', function) OnMouseMove (initialClickX, initialClick, mouse.x, mouse.A., initial ball, initial bell)}, false);}, false); Function OnMoiceMov (initialClickX, initialclick, mousex, mouse, initialball, initial bollywood) {ball.x = mousex + initialball - initialClickX; Ball.y = Mouse Y + Initial BallY - InitialClick; Attract (); }    

No comments:

Post a Comment