* Binds keys or key combos to an axis. The keys should be in the following order; up, down, left, right. If any * of the the binded key or key combos are active the callback will fire. The callback will be passed an array * containing two numbers. The first represents x and the second represents
(up, down, left, right, callback)
| 331 | * @param callback |
| 332 | */ |
| 333 | function bindAxis(up, down, left, right, callback) { |
| 334 | |
| 335 | function clear() { |
| 336 | if(typeof clearUp === 'function') { clearUp(); } |
| 337 | if(typeof clearDown === 'function') { clearDown(); } |
| 338 | if(typeof clearLeft === 'function') { clearLeft(); } |
| 339 | if(typeof clearRight === 'function') { clearRight(); } |
| 340 | if(typeof timer === 'function') { clearInterval(timer); } |
| 341 | } |
| 342 | |
| 343 | var axis = [0, 0]; |
| 344 | |
| 345 | if(typeof callback !== 'function') { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | //up |
| 350 | var clearUp = bindKey(up, function () { |
| 351 | if(axis[0] === 0) { |
| 352 | axis[0] = -1; |
| 353 | } |
| 354 | }, function() { |
| 355 | axis[0] = 0; |
| 356 | }).clear; |
| 357 | |
| 358 | //down |
| 359 | var clearDown = bindKey(down, function () { |
| 360 | if(axis[0] === 0) { |
| 361 | axis[0] = 1; |
| 362 | } |
| 363 | }, function() { |
| 364 | axis[0] = 0; |
| 365 | }).clear; |
| 366 | |
| 367 | //left |
| 368 | var clearLeft = bindKey(left, function () { |
| 369 | if(axis[1] === 0) { |
| 370 | axis[1] = -1; |
| 371 | } |
| 372 | }, function() { |
| 373 | axis[1] = 0; |
| 374 | }).clear; |
| 375 | |
| 376 | //right |
| 377 | var clearRight = bindKey(right, function () { |
| 378 | if(axis[1] === 0) { |
| 379 | axis[1] = 1; |
| 380 | } |
| 381 | }, function() { |
| 382 | axis[1] = 0; |
| 383 | }).clear; |
| 384 | |
| 385 | var timer = setInterval(function(){ |
| 386 | |
| 387 | //NO CHANGE |
| 388 | if(axis[0] === 0 && axis[1] === 0) { |
| 389 | return; |
| 390 | } |