SHARE
TWEET

Untitled

a guest Jun 18th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create:function (){
  2.         //var picIndex= this.game.rnd.integerInRange(1, 100);
  3.         //var key= 'backgroundLvl2'+picIndex;
  4.  
  5.         this.clickLock=false;
  6.         this.blockGroup= this.game.add.group();
  7.         var count=0;
  8.  
  9. for (var row=0;row<4;row++)
  10.     {
  11.         for (var col=0;col<4;col++)
  12.         {
  13.             var block=this.game.add.sprite(col*160,row*160,'backgroundLvl2');
  14.             block.ox=block.x;
  15.             block.oy=block.y;
  16.             block.inputEnabled= true;
  17.             block.events.onInputDown.add(this.pickBlock,this);
  18.             block.frame = count;
  19.             count++;
  20.             this.blockGroup.add(block);
  21.         }
  22.     }
  23.  
  24.     this.blockGroup.x=this.game.width/2-this.blockGroup.width/2;
  25.     this.blockGroup.y=this.game.height/2-this.blockGroup.height/2;
  26.  
  27.     this.endBlock=block;
  28.  
  29.     this.endBlock.loadTexture("SqB")
  30.         this.scramblePics();
  31.     },
  32.  
  33.  
  34.     pickBlock:function (block)
  35.     {
  36.         if(this.clickLock == true)
  37.         {
  38.             return;
  39.  
  40.         }
  41.         if(this.endBlock==block)
  42.         {
  43.             return;
  44.         }
  45.  
  46.  
  47.         var diffX = Math.abs(this.endBlock.x - block.x);
  48.         var diffY = Math.abs(this.endBlock.y-block.y);
  49.  
  50.         if( diffX>160 || diffY>160){
  51.             return;
  52.         }
  53.  
  54.         if (diffY == 160 && diffX==160 ){
  55.             return;
  56.         }
  57.  
  58.         block.bringToTop();
  59.  
  60.         this.clickLock=true;
  61.         var x1= block.x;
  62.         var y1= block.y;
  63.        // block.x= this.endBlock.x;
  64.        // block.y= this.endBlock.y;
  65.         //this.endBlock.y=y1;
  66.         //this.endBlock.x=x1;
  67.         this.moveBlock(block,this.endBlock.x,this.endBlock.y);
  68.         this.moveBlock(this.endBlock,x1,y1);
  69.  
  70.     },
  71.      
  72. scramblePics:function(){
  73.         for(var i=0;i<100;i++) {
  74.             var block1 = this.game.rnd.integerInRange(0, this.blockGroup.children.length - 1);
  75.             var block2 = this.game.rnd.integerInRange(0, this.blockGroup.children.length -1 );
  76.  
  77.             block1 = this.blockGroup.getChildAt(block1Index);
  78.             block2 = this.blockGroup.getChildAt(block2Index);
  79.             this.swapBlocks(block1, block2);
  80.         }
  81.     },
  82.      
  83. swapBlocks(block1,block2){
  84.         var tempX = block1.x;
  85.         var tempY =block1.y;
  86.  
  87.         block1.x=block2.x;
  88.         block1.y=block2.y;
  89.  
  90.         block2.x=tempX;
  91.         block2.y=tempY;
  92.     },
  93.  
  94.     moveBlock(block,xx,yy){
  95.  
  96.         var tw= this.game.add.tween(block).to({y:yy, x:xx},500, Phaser.Easing.Linear.None, true);
  97.         tw.onComplete.add(this.tweenDone,this)
  98.     },
  99.  
  100.     tweenDone:function () {
  101.         this.checkWin();
  102.         this.clickLock=false;
  103.     },
  104.  
  105.     checkWin(){
  106.         var win= true;
  107.  
  108.         this.blockGroup.forEach(function (block) {
  109.             if (block.x!=block.ox||block.y!=block.oy){
  110.                 win=false;
  111.             }
  112.         }.bind(this));
  113.  
  114.     },
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top