Shufflin' Bones: Part II

October 06, 2004

In Part I we discussed how we would handle collision detection in our game. Before we go any further, it would be nice to have something to collide, wouldn’t it? Let’s take a peek at how we’ll generate our set of dominoes.

In our game, we’ll have a Boneyard class which wll extend movieclip. Here’s a look at the init function within Boneyard which is responsible for creating and shuffling the set.


function init() {
 _collection = new Array();
 for(var i=0; i<=_setType; i++)
 {
  for(var j=0; j<=i; j++)
  {
   this.attachMovie("Domino", j + "_" + i, _collection.length + 100, {_leftPip:j, _rightPip:i});
   _collection.push(this[j + "_" + i]);
  }
 }
 shuffle();
}

_collection is a property of Boneyard: an array of Domino movieclip objects, which will be present along with Boneyard in the library. _setType is a numeric property that is set when creating an instance of Boneyard. There are 3 acceptable values: 6, 9, 12. These numbers represent the double-6, -9, and -12 domino sets respectively and are the common ones used in Mexican Train. Which set used depends on the number of players.

This would now be a good time to fully understand the make-up of a domino set if you don’t already. To make matters short, “…(e)very set of dominoes includes all possible combinations of two numbers, from zero (blank) up to the highest number of pips in the set (for example, 12 in a double-12 set), as well as a double for each suit. Each combination of pips occurs only once in a set. A standard double-6 domino set consists of 28 tiles: 7 doubles and 21 singles. (Introduction to Dominoes by Puremco World’s Finest Domino Games)”. Reference the aforementioned link for further explaination.

So, how can we create such a collection in ActionScript?

This is where it pays off to be married to a mathematician, because I knew that’s exactly what it would take. A way to look at all the pieces in a set would be in the form of a matrix. Consider for example the double-6 set:

pip 0 1 2 3 4 5 6
0 0-0
1 0-1 1-1
2 0-2 1-2 2-2
3 0-3 1-3 2-3 3-3
4 0-4 1-4 2-4 3-4 4-4
5 0-5 1-5 2-5 3-5 4-5 5-5
6 0-6 1-6 2-6 3-6 4-6 5-6 6-6

If you were to count them all up, you would see we have all 28 bones represented. We then use a couple of for loops to move through the matrix and create an instance of the Domino moviecip for each bone using attachMovie. It’s given a unique name, depth, and the additional pip values in an object which are needed to generate the new instance. Afterwards it’s pushed into the _collection array.

“So what about the shuffle() function?”, you ask. Part III is coming soon.

Posted in Flash/ActionScript, Games

Comments

Categories

Search the Archive

Monthly Archive

Recent Entries

Texas Toast™ Technologies