Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define("Controllable", ['Game','Config','Controls'],
- function( Game, Config, Controls ) {
- var Controllable = function (){
- this.game = Game;
- this.controls = new Controls();
- this.isActive = true;
- this.jumpCount = 0;
- }
- Controllable.prototype = {
- move: function(){
- this.sprite.body.velocity.x = 0;
- if(this.isActive){
- // Movement directions
- if(this.isRightPressed()){
- this.sprite.scale.x = 1;
- this.sprite.body.velocity.x = this._speed;
- this.sprite.animations.play('run');
- }else if(this.isLeftPressed()){
- this.sprite.scale.x = -1;
- this.sprite.body.velocity.x = -1 * this._speed;
- this.sprite.animations.play('run');
- }
- if(this.isJumpPressed() && !this.sprite.body.touching.down){
- this.sprite.frame = 4;
- }
- if(this.sprite.body.touching.down){
- this.jumpCount = 0;
- if(this.sprite.frame === 4){
- this.sprite.frame = 0;
- }
- }
- }
- },
- isJumpPressed: function(){
- return this.controls.UP.isDown || this.controls.SPACEBAR.isDown;
- },
- isJumpReleased: function(){
- return this.controls.UP.isUp || this.controls.SPACEBAR.isUp;
- },
- jumpCheck: function(){
- if(this.isActive){
- if(this.jumpCount < 2){
- this.jump();
- this.jumpCount++;
- }
- }
- },
- jump: function(){
- this.sprite.body.velocity.y = -1 * this._jump;
- },
- isLeftPressed: function(){
- return this.controls.LEFT.isDown;
- },
- isRightPressed: function(){
- return this.controls.RIGHT.isDown;
- },
- stopRunning: function(){
- this.sprite.animations.stop('run');
- this.sprite.frame = 0;
- },
- initOtherEvents: function(){
- this.controls.UP.onDown.add(this.jumpCheck, this);
- this.controls.SPACEBAR.onDown.add(this.jumpCheck, this);
- this.controls.LEFT.onUp.add(this.stopRunning, this);
- this.controls.RIGHT.onUp.add(this.stopRunning, this);
- }
- };
- return Controllable;
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement