Advertisement
Guest User

Untitled

a guest
Feb 19th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. define("Controllable", ['Game','Config','Controls'],
  2.  
  3. function( Game, Config, Controls ) {
  4.  
  5. var Controllable = function (){
  6. this.game = Game;
  7. this.controls = new Controls();
  8.  
  9. this.isActive = true;
  10.  
  11. this.jumpCount = 0;
  12.  
  13. }
  14.  
  15. Controllable.prototype = {
  16.  
  17. move: function(){
  18.  
  19. this.sprite.body.velocity.x = 0;
  20.  
  21. if(this.isActive){
  22. // Movement directions
  23. if(this.isRightPressed()){
  24. this.sprite.scale.x = 1;
  25. this.sprite.body.velocity.x = this._speed;
  26. this.sprite.animations.play('run');
  27. }else if(this.isLeftPressed()){
  28. this.sprite.scale.x = -1;
  29. this.sprite.body.velocity.x = -1 * this._speed;
  30. this.sprite.animations.play('run');
  31. }
  32.  
  33. if(this.isJumpPressed() && !this.sprite.body.touching.down){
  34. this.sprite.frame = 4;
  35. }
  36.  
  37. if(this.sprite.body.touching.down){
  38. this.jumpCount = 0;
  39. if(this.sprite.frame === 4){
  40. this.sprite.frame = 0;
  41. }
  42. }
  43.  
  44. }
  45.  
  46. },
  47.  
  48. isJumpPressed: function(){
  49. return this.controls.UP.isDown || this.controls.SPACEBAR.isDown;
  50. },
  51.  
  52. isJumpReleased: function(){
  53. return this.controls.UP.isUp || this.controls.SPACEBAR.isUp;
  54. },
  55.  
  56. jumpCheck: function(){
  57. if(this.isActive){
  58. if(this.jumpCount < 2){
  59. this.jump();
  60. this.jumpCount++;
  61. }
  62. }
  63. },
  64.  
  65. jump: function(){
  66. this.sprite.body.velocity.y = -1 * this._jump;
  67. },
  68.  
  69. isLeftPressed: function(){
  70. return this.controls.LEFT.isDown;
  71. },
  72.  
  73. isRightPressed: function(){
  74. return this.controls.RIGHT.isDown;
  75. },
  76.  
  77. stopRunning: function(){
  78. this.sprite.animations.stop('run');
  79. this.sprite.frame = 0;
  80. },
  81.  
  82. initOtherEvents: function(){
  83. this.controls.UP.onDown.add(this.jumpCheck, this);
  84. this.controls.SPACEBAR.onDown.add(this.jumpCheck, this);
  85.  
  86. this.controls.LEFT.onUp.add(this.stopRunning, this);
  87. this.controls.RIGHT.onUp.add(this.stopRunning, this);
  88. }
  89.  
  90.  
  91. };
  92.  
  93. return Controllable;
  94.  
  95. }
  96.  
  97. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement