home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Puzzle / clicknslide_offsite.swf / scripts / DefineSprite_239 / frame_2 / DoAction.as
Encoding:
Text File  |  2007-03-28  |  5.3 KB  |  254 lines

  1. function buildPuzzle()
  2. {
  3.    blockcount = 0;
  4.    var startx = 0;
  5.    var starty = 0;
  6.    blocksize = 300 / puzzlesize;
  7.    y = 0;
  8.    while(y < puzzlesize)
  9.    {
  10.       x = 0;
  11.       while(x < puzzlesize)
  12.       {
  13.          newname = "block" + y + x;
  14.          block_count++;
  15.          duplicateMovieClip("block",newname,16384 + block_count);
  16.          this[newname]._x = startx + blocksize * x;
  17.          this[newname]._y = starty + blocksize * y;
  18.          this[newname].x = x;
  19.          this[newname].y = y;
  20.          this[newname].photo.gotoAndStop(_parent.pv.preview._currentframe);
  21.          this[newname].photo._x = blocksize * x * -1;
  22.          this[newname].photo._y = blocksize * y * -1;
  23.          this[newname].gotoAndStop(puzzlesize - 2);
  24.          x++;
  25.       }
  26.       y++;
  27.    }
  28.    this[newname]._alpha = 0;
  29.    this[newname]._visible = false;
  30.    blankblock = newname;
  31. }
  32. function checkPuzzle()
  33. {
  34.    wrong = 0;
  35.    y = 0;
  36.    while(y < puzzlesize)
  37.    {
  38.       x = 0;
  39.       while(x < puzzlesize)
  40.       {
  41.          if(puzzlearray[y][x] != "block" + y + x)
  42.          {
  43.             wrong++;
  44.          }
  45.          x++;
  46.       }
  47.       y++;
  48.    }
  49.    _parent.percentcompleted = Math.round(100 * ((puzzlesize * puzzlesize - wrong) / (puzzlesize * puzzlesize))) + "%";
  50.    if(wrong <= 0)
  51.    {
  52.       return true;
  53.    }
  54.    return false;
  55. }
  56. function reset()
  57. {
  58.    y = 0;
  59.    while(y < puzzlesize)
  60.    {
  61.       x = 0;
  62.       while(x < puzzlesize)
  63.       {
  64.          this["block" + y + x].removeMovieClip();
  65.          x++;
  66.       }
  67.       y++;
  68.    }
  69.    _parent.movecount = 0;
  70.    _parent.percentcompleted = "";
  71. }
  72. function slide(b)
  73. {
  74.    if(solved == true)
  75.    {
  76.       return undefined;
  77.    }
  78.    if(this[b].motion == true)
  79.    {
  80.       return undefined;
  81.    }
  82.    if(b == blankblock)
  83.    {
  84.       return undefined;
  85.    }
  86.    var cx = this[b].x;
  87.    var cy = this[b].y;
  88.    var bx = this[blankblock].x;
  89.    var by = this[blankblock].y;
  90.    if(cx != bx)
  91.    {
  92.       if(cy != by)
  93.       {
  94.          return undefined;
  95.       }
  96.       if(cx <= bx)
  97.       {
  98.          if(cx < bx)
  99.          {
  100.             swapBlocks(-1,0,b);
  101.          }
  102.       }
  103.       else
  104.       {
  105.          swapBlocks(1,0,b);
  106.       }
  107.    }
  108.    else if(cy <= by)
  109.    {
  110.       if(cy < by)
  111.       {
  112.          swapBlocks(0,-1,b);
  113.       }
  114.    }
  115.    else
  116.    {
  117.       swapBlocks(0,1,b);
  118.    }
  119.    _parent.movecount++;
  120.    if(checkPuzzle())
  121.    {
  122.       this[blankblock].reveal = true;
  123.       solved = true;
  124.       RevealPicture();
  125.       _parent.sounds.gotoAndPlay("win");
  126.    }
  127. }
  128. function RevealPicture()
  129. {
  130.    y = 0;
  131.    while(y < puzzlesize)
  132.    {
  133.       x = 0;
  134.       while(x < puzzlesize)
  135.       {
  136.          this["block" + y + x].outline.gotoAndPlay("fade");
  137.          x++;
  138.       }
  139.       y++;
  140.    }
  141. }
  142. function swapBlocks(x, y, b)
  143. {
  144.    var bx = this[blankblock].x;
  145.    var by = this[blankblock].y;
  146.    var swapblock = puzzlearray[by + y][bx + x];
  147.    var tempx = this[blankblock]._x;
  148.    var tempy = this[blankblock]._y;
  149.    this[blankblock]._x = this[swapblock]._x;
  150.    this[blankblock]._y = this[swapblock]._y;
  151.    this[swapblock].newx = tempx;
  152.    this[swapblock].newy = tempy;
  153.    this[swapblock].x = bx;
  154.    this[swapblock].y = by;
  155.    this[swapblock].motion = true;
  156.    this[swapblock].mx = x;
  157.    this[swapblock].my = y;
  158.    this[blankblock].x = bx + x;
  159.    this[blankblock].y = by + y;
  160.    puzzlearray[by][bx] = swapblock;
  161.    puzzlearray[by + y][bx + x] = blankblock;
  162.    if(swapblock != b)
  163.    {
  164.       swapBlocks(x,y,b);
  165.    }
  166. }
  167. function shuffle()
  168. {
  169.    shufflecomplete = false;
  170.    j = 0;
  171.    while(j <= 500)
  172.    {
  173.       bx = this[blankblock].x;
  174.       by = this[blankblock].y;
  175.       do
  176.       {
  177.          xory = random(2);
  178.          if(xory == 0)
  179.          {
  180.             rx = bx;
  181.             ry = random(2);
  182.             if(ry != 0)
  183.             {
  184.                ry = by + 1;
  185.             }
  186.             else
  187.             {
  188.                ry = by - 1;
  189.             }
  190.          }
  191.          else
  192.          {
  193.             ry = by;
  194.             rx = random(2);
  195.             if(rx == 0)
  196.             {
  197.                rx = bx - 1;
  198.             }
  199.             else
  200.             {
  201.                rx = bx + 1;
  202.             }
  203.          }
  204.       }
  205.       while(rx >= puzzlesize || rx < 0 || ry < 0 || ry >= puzzlesize);
  206.       
  207.       swapblock = puzzlearray[ry][rx];
  208.       puzzlearray[ry][rx] = blankblock;
  209.       this[blankblock].x = rx;
  210.       this[blankblock].y = ry;
  211.       puzzlearray[by][bx] = swapblock;
  212.       this[swapblock].x = bx;
  213.       this[swapblock].y = by;
  214.       j++;
  215.    }
  216.    shufflecomplete = true;
  217.    y = 0;
  218.    while(y < puzzlesize)
  219.    {
  220.       x = 0;
  221.       while(x < puzzlesize)
  222.       {
  223.          this[puzzlearray[y][x]]._x = blocksize * x;
  224.          this[puzzlearray[y][x]]._y = blocksize * y;
  225.          x++;
  226.       }
  227.       y++;
  228.    }
  229.    checkPuzzle();
  230.    solved = false;
  231.    _parent.timerclip.startTime = getTimer();
  232.    _parent.timerclip.seconds = 0;
  233.    _parent.timerclip.minutes = 0;
  234. }
  235. function BuildArray()
  236. {
  237.    puzzlearray = new Array();
  238.    y = 0;
  239.    while(y < puzzlesize)
  240.    {
  241.       puzzlearray[y] = new Array();
  242.       x = 0;
  243.       while(x < puzzlesize)
  244.       {
  245.          thisblock = "block" + y + x;
  246.          tx = this[thisblock].x;
  247.          ty = this[thisblock].y;
  248.          puzzlearray[ty][tx] = this[thisblock]._name;
  249.          x++;
  250.       }
  251.       y++;
  252.    }
  253. }
  254.