Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

9. Game #8, Treasure Chest > Time for action — checking neighboring horizontal ...

Time for action — checking neighboring horizontal tiles

Time for action — checking neighboring horizontal tiles We will create two methods for checking the neighboring tiles, as we need to do this for each axis (x and y), separately. Inside the game class, add a new method called CheckGemsX. As parameters, it will take the column and row, the gem type, and a mark flag. Method CheckGemsX:Int(column:Int, row:Int, gem:Int, mark:Bool = False) First, add a local found variable of the type INT. It will be set once we find similar gems. Local found:Int = 0 Initialize the slot. tileMap[column-1][row-1] = -1 Check if column is greater than 1. It means there must be gems on the left. If column > 1 Then Start a FOR loop, stepping backwards from column -1 to the first gem on the left. For Local c:Int = (column-1) To 1 Step -1 If the gem found isn't the same, exit the FOR loop. If tileMap[c-1][row-1] <> gem Then Exit If the mark flag was set, fill the value 99 into the tile slot. It is used if we want to mark tiles to delete them later. If mark Then tileMap[c-1][row-1] = 99 Else If the flag isn't set, just count the gems by raising found by 1. found += 1 Endif Close the FOR loop, and the first IF check. Next Endif Now to the gems on the right. First, check if column isn't greater than the field columns of the game class. If column < columns Then Next start a FOR loop, ranging from column+1 to columns For Local c:Int = (column+1) To columns Check if the found gem is the same. If not, exit the FOR loop. If tileMap[c-1][row-1] <> gem Then Exit If the mark flag is set, fill the value 99 into the slot. If mark Then tileMap[c-1][row-1] = 99 Else If not, raise found by 1. found += 1 Endif Close the FOR loop and the IF check. Next Endif If the mark flag was set, fill the value 99 into the slot of the actual gem, too. You want to delete it also, not only the neighboring tiles. If mark Then tileMap[column-1][row-1] = 99 Else If not, restore the slot with the gem parameter. tileMap[column-1][row-1] = gem Endif Return the number of gems we have found, and then close the method. Return found End

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint