notalentgeek

Phaser - StatePlay

Aug 17th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var statePlay = {
  2.  
  3.     create:                         function(){
  4.  
  5.         game.physics.arcade.gravity.y   = 1200;
  6.  
  7.         this.map                        = game.add.tilemap('Tilemap1');
  8.         this.map.addTilesetImage        ('Tilemap1', 'ImageTilemap1');
  9.         this.map.setCollisionByExclusion([0], true, 'LayerBlock');
  10.         this.layerBackground            = this.map.createLayer('LayerBackground');
  11.         this.layerBlock                 = this.map.createLayer('LayerBlock');
  12.         this.keyboard                   = game.input.keyboard.createCursorKeys();
  13.  
  14.         this.coinGroup              = game.add.group();
  15.         this.coinGroup.enabledBody  = true;
  16.         this.map.createFromObjects  ('LayerObject', 4, 'ImageCoin', 0, true, false, this.coinGroup);
  17.  
  18.         //this.ObjectCoinCreate();
  19.         //this.ObjectPlayerCreate();
  20.  
  21.     },
  22.  
  23.     update:                         function(){
  24.  
  25.         //this.CollisionObject();
  26.         //this.ObjectPlayerUpdate();
  27.         //this.OverlapObject();
  28.  
  29.     },
  30.  
  31.     CollisionObject:                function(){
  32.  
  33.         game.physics.arcade.collide(this.player,            this.layerBlock);
  34.         for(var i = 0; i < this.coinArray.length; i ++){
  35.             game.physics.arcade.collide(this.coinArray[i],  this.layerBlock);
  36.         }
  37.  
  38.     },
  39.  
  40.     FindGameObjectsByType:          function(_layer, _map, _type){
  41.  
  42.         var findObjects = new Array();
  43.         _map.objects[_layer].forEach(
  44.  
  45.             function(_element){
  46.  
  47.                 if(_element.properties.type == _type){
  48.  
  49.                     _element.y -= _map.tileHeight;
  50.                     findObjects.push(_element);
  51.  
  52.                 }
  53.  
  54.             }
  55.  
  56.         );
  57.         return findObjects;
  58.  
  59.     },
  60.  
  61.     PlayerCollectsCoin:             function(_player, _coin){ _coin.destroy(true); },
  62.  
  63.     ObjectCoinCreate:               function(){
  64.  
  65.         var findCoin            = this.FindGameObjectsByType('LayerObject', this.map, 'coin');
  66.         this.coinArray          = new Array(findCoin.length);
  67.         for(var i = 0; i < this.coinArray.length; i ++){
  68.  
  69.             this.coinArray[i]                   = game.add.sprite(findCoin[i].x, findCoin[i].y, 'ImageCoin');
  70.             this.coinArray[i].enabledBody       = true;
  71.             game.physics.arcade.enable          (this.coinArray[i]);
  72.             this.coinArray[i].body.immovable    = true;
  73.             this.coinArray[i].body.moves        = false;
  74.  
  75.         }
  76.  
  77.     },
  78.  
  79.     ObjectPlayerCreate:             function(){
  80.  
  81.         var findPlayer                  = this.FindGameObjectsByType('LayerObject', this.map, 'playerPortal');
  82.         this.player                     = game.add.sprite(findPlayer[0].x, findPlayer[0].y, 'ImagePlayer');
  83.         this.player.enabledBody         = true;
  84.         game.physics.arcade.enable      (this.player);
  85.  
  86.     },
  87.  
  88.     ObjectPlayerUpdate:             function(){
  89.  
  90.         if      (this.keyboard.left.isDown)     { this.player.body.velocity.x = -200; }
  91.         else if (this.keyboard.right.isDown)    { this.player.body.velocity.x = 200; }
  92.         else                                    { this.player.body.velocity.x = 0; }
  93.  
  94.         if(
  95.  
  96.             this.keyboard.up.isDown &&
  97.             this.player.body.onFloor()
  98.  
  99.         ){ this.player.body.velocity.y = -500; }
  100.  
  101.     },
  102.  
  103.     OverlapObject:                  function(){
  104.  
  105.         for(var i = 0; i < this.coinArray.length; i ++){
  106.             game.physics.arcade.overlap(this.player, this.coinArray[i], this.PlayerCollectsCoin, null, this);
  107.         }
  108.  
  109.     }
  110.  
  111. };
Advertisement
Add Comment
Please, Sign In to add comment