Swapping gems

Swapping gems is the key of the game, as this allows the player to create successful streaks.

The idea: It takes two steps to swap two gems:

  • swapping their values in the array
  • swapping the position of their DisplayObjects

As you may see, once you know the two gems can be swapped, it's very easy.

The development: As usual we are making a set of functions to do the dirty jobs and keeping our main functions clear and easy to read.

First, we need a function to swap two elements in jewels array.

private function swapJewelsArray(row1:uint,col1:uint,row2:uint,col2:uint):void {
var tmp:uint=jewels[row1][col1];
jewels[row1][col1]=jewels[row2][col2];
jewels[row2][col2]=tmp;
}

The function is so easy that there's no need to comment it.

Another, ...

Get Flash Game Development by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.