1 Reply Latest reply on Jan 4, 2009 10:21 PM by johnypeter

    Where am I going wrong?

    Nigs
      I am trying to create a click and slide puzzle for my site and after hours of checking I am still getting errors;

      function initGame () {
      // set the horizontal and vertical distance
      // between tiles
      tileDist = 100;

      // set all tiles in exactly the correct spot
      for (x=1;x<=4;x++) {
      for (y=0;y<=3;y++) {
      tile = x+y*4;
      _root["tile"+tile]._x = x*tileDist;
      _root["tile"+tile]._y = y*tileDist+tileDist;
      }
      }

      // make 100 random but valid moves
      for(tilenum=0;tilenum<100;tilenum++) {
      do {
      // pick a random tile
      tile = "tile"+(random(15)+1);
      // see whether there is an empty space near it
      emptySpace = findEmpty(tile);
      // keep looping until a tile is found that
      // has an empty space near it
      } while ( emptySpace == "none" );

      // move this tile to the empty space
      moveTile(tile,findEmpty(tile));
      }
      }

      // given a tile, see if the empty space is near it
      function findEmpty (tile) {
      // get location of tile
      tilex = _root[tile]._x;
      tiley = _root[tile]._y;

      // see whether there is a tile to the left
      if (tilex > tileDist) {
      if (!tileThere(tilex-tileDist, tiley)) {
      return("left");
      }
      }

      // see whether there is a tile to the right
      if (tilex < tileDist*4) {
      if (!tileThere(tilex+tileDist, tiley)) {
      return("right");
      }
      }

      // see whether there is a tile above
      if (tiley > tileDist) {
      if (!tileThere(tilex, tiley-tileDist)) {
      return("above");
      }
      }

      // see whether there is a tile below
      if (tiley < tileDist*4) {
      if (!tileThere(tilex, tiley+tileDist)) {
      return("below");
      }
      }

      // tiles are in all directions
      return("none");
      }

      // check to see whether there is a tile at a certain location
      function tileThere (thisx, thisy) {
      // loop through tiles
      for (i=1;i<=15;i++) {
      // see if x matches
      if (_root["tile"+i]._x == thisx) {
      // se if y matches
      if (_root["tile"+i]._y == thisy) {
      return true;
      }
      }
      }

      // no tile there
      return false;
      }

      // move a tile in a certain direction
      function moveTile (tile, direction) {
      if (direction == "above") {
      _root[tile]._y -= tileDist;
      } else if (direction == "below") {
      _root[tile]._y += tileDist;
      } else if (direction == "left") {
      _root[tile]._x -= tileDist;
      } else if (direction == "right") {
      _root[tile]._x += tileDist;
      }
      }

      // utility function to see on which tile the
      // player clicked
      function tileUnderMouse () {
      for (i=1; i<=15; i++) {
      if (_root["Tile"+i].hitTest(_xmouse, _ymouse)) {
      return (i);
      }
      }
      }

      initGame();
      stop();

      The code above has been taken and adapted (unsuccessfully, obviously) from the Flash MX book eventhough I am using Flash 8. From a novice point of view I would not have thought that there was much difference, but again I am obviously wrong.

      The problem I am finding is that when I CTRL/Return to preview it, it takes ages and then I get the message that the movie is causing the Flash Player to run slowly and asks if I want to continue. If I choose yes, this message cycles around and round. If I chose no then I get the file to open but all the pieces are not set right and nothing moves.

      Any help will be hugely appreciated!

      Cheers

      Nig
        • 1. Re: Where am I going wrong?
          johnypeter Level 1
          Hi There,
          I am bit confused with your logic behind the do-while loop.
          I think it might be the misuse of logic in the for and while loop..
          try changing the loop format and look at the same code...
          if possible , post your fla here....

          Regards,
          - J