Note that there are some explanatory texts on larger screens.

plurals
  1. POFilling open spaces in a grid top down
    primarykey
    data
    text
    <p>I am writing a match three engine and I succeed in creating the matching with using huge loops to find the matching items. Any ideas on how to fill the empty spaces with the items ( dropping down into the empty spaces ) and creating new items without excessive looping and if statements?</p> <p>Here is my relavant code so far.</p> <pre><code>public var rows:uint = 8; public var cols:uint = 7; public var cell:Array = new Array(); public var plot:Array = new Array(); public var height:int; public var width:int; public var relativePositions:Array = [{name:'top', position:-1}, {name:'bottom', position:1}, {name:'left', position:rows*-1}, {name:'right', position:rows*1}]; public var dictionary:Dictionary = new Dictionary(); public var matches:Array = new Array(); public function createGrid(target:*, displayObject:*, spacer:int) : void { var iterator:uint = 0; for(var c:uint = 0;c&lt;cols;c++){ for(var r:uint = 0;r&lt;rows;r++){ cell[iterator] = createGamePiece(); Sprite(cell[iterator]).name = String(iterator); Sprite(cell[iterator]).addEventListener(MouseEvent.CLICK, _handleGamePiece_CLICK); Sprite(cell[iterator]).addEventListener(MouseEvent.MOUSE_OVER, _handleGamePiece_MOUSE_OVER); Sprite(cell[iterator]).addEventListener(MouseEvent.MOUSE_OUT, _handleGamePiece_MOUSE_OUT); cell[iterator].y = cell[iterator].height * r + (spacer*r); cell[iterator].x = cell[iterator].width * c + (spacer*c); GamePiece(cell[iterator]).positionX = cell[iterator].x; GamePiece(cell[iterator]).positionY = cell[iterator].y; GamePiece(cell[iterator]).positionRow = r; GamePiece(cell[iterator]).positionCol = c; target.addChild(cell[iterator]); dictionary[String(iterator)] = cell[iterator] iterator++ } } } public function findRelativeMatches(targetSprite:Sprite) : void { targetSprite.alpha = .5; var rootPosition:Number = Number(targetSprite.name); for ( var i:int = 0; i &lt; relativePositions.length; i ++ ) { var key:String = String(rootPosition + relativePositions[i].position); // to do &gt;&gt; Not hardcoded to 'Pig' if (findSprite(key) != null &amp;&amp; GamePiece(targetSprite).color == GamePiece(findSprite(key)).color &amp;&amp; GamePiece(findSprite(key)).found == false) { var sprite:Sprite = findSprite(key); sprite.alpha = .5; GamePiece(sprite).found = true; matches.push(sprite); findRelativeMatches(sprite); }; }; targetSprite.addEventListener(MouseEvent.MOUSE_OUT, function() : void { if ( matches.length != 0 ) { for ( var j:int = 0 ; j &lt; matches.length ; j++ ) { Sprite(matches[j]).alpha = 1; GamePiece(matches[j]).found = false; } matches.splice(0); } }); } public function findSprite(key:String) : Sprite { var sprite:Sprite; dictionary[key] != undefined ? sprite = dictionary[key] : null; return sprite; } protected function _handleGamePiece_CLICK(event:MouseEvent):void { for ( var j:int = 0 ; j &lt; matches.length ; j++ ) { var sprite:Sprite = matches[j]; view.removeChild(matches[j]); } matches.splice(0); } public function createGamePiece() : Sprite { var gamePiece:GamePiece = new GamePiece(); return gamePiece; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload