dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: determining tile state

  1. #1
    Hey all, I've been working on a new game for a few days now and am almost done, except for one small problem.

    The game use a tile matrix of 10 X 10. The tile MC's are numbered from 1-100. I need to right I search function that will determine if the tiles next to the one that is clicked on are the same, and if so it will add the name to an array and test that tile also.

    I can make it work for the tiles directly next to it but not for the tiles further on.

    Any ideas would be great, any code would be better

    Cheers,
    Clayton

  2. #2
    Unregistered User dnalogic's Avatar
    Join Date
    Feb 2001
    Location
    UK
    Posts
    146
    Sounds like you want to WRITE some sort of flood-fill algorithm?
    A neat way to do it would be to use some sort of recursive function.

    When you click on tile n:
    Code:
    // Find the matching tiles
    matchingtile=new Array();
    fncCheckTile(n,tile[n]);
    
    // List the matching tiles
    for(var n=1;n<=100;n++){
        if(matchingtile[n]){
            trace(n);
        }
    }
    This assumes that you already have an array called tile that has elements 1 to 100 containing the 'type' of each tile MC.

    Here is a recursive function to check the neighbouring tiles:
    Code:
    function fncCheckTile(n,m){
        if(tile[n]==m && !matchingtile[n]){ // check tile matches and that it hasn't been checked before
            matchingtile[n]=true; // add to array
            fncCheckTile(n-1,m); // check left tile
            fncCheckTile(n+1,m); // check right tile
            fncCheckTile(n-10,m); // check above tile
            fncCheckTile(n+10,m); // check below tile
        }
    }
    Note: The above function won't work properly for the edges, becuase at the end it will wrap around to the next row, so you need to put a few if statements in there.

    Oh, yeah, I assumed that you have 1 to 10 across the top row, then 11 to 20 across the next row down, etc, which is why 'n+10' is for the below tile, and 'n+1' is for the right tile, etc.

    None of the code is tested, but I'm sure you know what I'm getting at.


    Regards,
    Dean Alexandrou.

  3. #3

    thanks for the help

    I didn't end up using your meathod but would like to say thanks for your effort.

    Cheers,
    Clayton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center