Boom – the troublesome gem explosion

Sometimes less is more they say, and well, sometimes they are right. At the very start of the project I had created this nice little function for a special gem. When the user creates a match using the special gem all surrounding stones will “explode” – disappear. BOOM. This is actually very common in match 3 games. but somehow for me it would not want to work.

Well the problems were

1. Accessing the stones. The stones had to be one row higher one left etc.. but what it there is no stone because that is where the grid is finished?
Check if there is something first one could say… if that would be so easy in Flash because this exactly is the next problem

2. How can you tell flash to remove something IF it is there?
because

if sprite.contains(“myobject”) does not work if “myobject” never existed or is not defined … so if I am looking for a stone in there that has never been defined Flash will through nice error “The supplied index is out of bounds” and the like. What you could do and I did eventually was to store all stones in an array and then check if  the array position is != null.  But then again, you will have a problem in a very specific case:

The function does not work if the stone is at the very top left edge! And why is that?
Because the calculated row for some of the stones would be negative, like   piece.row = -1  and you can’t search and array for a negative index… so this is the precious function I had after months of development and going over and over it again:

		public function specialfourgem(piece:Piece) {

			MovieClip(root).playSound(burningremoveinst);

			var newrow:Number = piece.row
			var newcol:Number = piece.col

			//trace(piece.col - 1);
			//
			//   a b c
			//	 d e f
			//   g h i 

			if (( piece.col - 1 >= 0 ) && ( piece.row - 1 >= 0))
				{
				trace ("a");
				gameSprite.removeChild(grid[piece.col - 1 ][piece.row - 1]);

					if (usedtiles[piece.col - 1 ][piece.row - 1]!= null)
						{
							trace("there is something")
							bgsprite.removeChild(usedtiles[piece.col - 1 ][piece.row - 1])
							usedtiles[piece.col - 1 ][piece.row - 1] = null;

							}
				affectAbove(grid[piece.col - 1 ][piece.row - 1]);
			//	grid[piece.col - 1][piece.row - 1] = null;

				}

			if (( piece.col >= 0 ) && ( piece.row - 1 >= 0)) {
				trace ("b");
				gameSprite.removeChild(grid[piece.col][piece.row - 1]);

				if (usedtiles[piece.col][piece.row - 1]!= null)
						{
							bgsprite.removeChild(usedtiles[piece.col][piece.row - 1])
							usedtiles[piece.col][piece.row - 1] = null; 

							}

				affectAbove(grid[piece.col][piece.row - 1]);
		//		grid[piece.col][piece.row - 1] = null;

				}
			if (( piece.col + 1 <= 9 ) && ( piece.row - 1 >= 0)) {
				trace ("c");
				gameSprite.removeChild(grid[piece.col + 1 ][piece.row - 1])

						if (usedtiles[piece.col + 1][piece.row - 1]!= null)
						{
							bgsprite.removeChild(usedtiles[piece.col + 1][piece.row - 1])
							usedtiles[piece.col + 1][piece.row - 1] = null;

							}
				affectAbove(grid[piece.col + 1][piece.row - 1]);
		//		grid[piece.col + 1][piece.row - 1] = null;

				}
			if (( piece.col -1  >= 0 ) && ( piece.row >= 0)) {
				gameSprite.removeChild(grid[piece.col - 1 ][piece.row])

					if (usedtiles[piece.col - 1][piece.row]!= null)
						{
							bgsprite.removeChild(usedtiles[piece.col - 1][piece.row ])
							usedtiles[piece.col - 1][piece.row] = null;

							}

				affectAbove(grid[piece.col - 1 ][piece.row]);
		//		grid[piece.col - 1][piece.row] = null;
				trace ("d");

				}

			if (( piece.col + 1 <= 9 ) && ( piece.row >= 0)) {
				trace ("f");
				gameSprite.removeChild(grid[piece.col + 1 ][piece.row]);

						if (usedtiles[piece.col + 1][piece.row]!= null)
							{
							trace("this is the provlem");
							bgsprite.removeChild(usedtiles[piece.col + 1][piece.row ])
							usedtiles[piece.col + 1][piece.row] = null;

							}

				affectAbove(grid[piece.col + 1][piece.row]);
		//		grid[piece.col + 1][piece.row] = null;

				}
			if (( piece.col -1 >= 0 ) && ( piece.row + 1 <= 7)) {
				trace ("g");
				gameSprite.removeChild(grid[piece.col - 1 ][piece.row + 1])

				if (usedtiles[piece.col - 1][piece.row + 1]!= null)
							{
							bgsprite.removeChild(usedtiles[piece.col - 1][piece.row + 1 ])
							usedtiles[piece.col - 1][piece.row + 1] = null;

							}

				affectAbove(grid[piece.col - 1][piece.row + 1]);
	//			grid[piece.col - 1][piece.row + 1] = null;

				}
			if (( piece.col >= 0 ) && ( piece.row + 1 <= 7)) {
				trace ("h");
				gameSprite.removeChild(grid[piece.col ][piece.row + 1])

				if (usedtiles[piece.col][piece.row + 1]!= null)
							{
							bgsprite.removeChild(usedtiles[piece.col][piece.row + 1])
							usedtiles[piece.col][piece.row + 1] = null;

							}

				affectAbove(grid[piece.col][piece.row + 1]);
	//			grid[piece.col][piece.row + 1] = null;

				}

			if (( piece.col + 1 <= 9 ) && ( piece.row + 1 <= 9)) {
				trace ("i");
				gameSprite.removeChild(grid[piece.col + 1 ][piece.row + 1])

				if (usedtiles[piece.col + 1][piece.row + 1]!= null)
							{
							bgsprite.removeChild(usedtiles[piece.col + 1][piece.row + 1 ])
							usedtiles[piece.col + 1][piece.row + 1] = null;

							}
				affectAbove(grid[piece.col + 1][piece.row + 1]);
	//			grid[piece.col + 1][piece.row + 1] = null;
				}
			gameSprite.removeChild(grid[piece.col][piece.row]);
			affectAbove(piece);

			if (usedtiles[piece.col][piece.row]!= null)
						{
						bgsprite.removeChild(usedtiles[piece.col][piece.row])
						usedtiles[piece.col][piece.row] = null;
					}

			}		

So in the beginning I said, “Less is more” … well this masterpiece unfortunately did not work properly either…maybe some notation problem somewhere because to be honest, this is really hard to read. So in a moment of clarity I suddenly managed to come up with this

public function specialfourgem(piece:Piece) {

			addScore(200);
			MovieClip(root).playSound(burningremoveinst);

			for (var i:int = -1; i < 2; i++) {
				for (var j:int = -1; j < 2; j++) {

					if ( (piece.col + i >= 0) && (piece.row + j >= 0) && (grid[piece.col + i][piece.row + j]) != ( null || undefined )) {

					//	trace("I entered")
						//trace ("imdoing" +(piece.col + i) +"and" +(piece.row + j))

						var temppiece:Piece = Piece(grid[piece.col + i][piece.row + j]);

						gameSprite.removeChild(temppiece);
						grid[temppiece.col][temppiece.row] = null;
						affectAbove(temppiece);

						if (usedtiles[temppiece.col][temppiece.row] != null || undefined) {

							bgsprite.removeChild(usedtiles[temppiece.col][temppiece.row]);
							usedtiles[temppiece.col][temppiece.row] = null;

							}

						}

				}

And it works….stunned..

Print Friendly

Leave a Reply


5 * four =