Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8480605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:20:27+00:00 2026-06-10T19:20:27+00:00

I am writing a match three engine and I succeed in creating the matching

  • 0

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?

Here is my relavant code so far.

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<cols;c++){ 
            for(var r:uint = 0;r<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 < relativePositions.length; i ++ ) {
            var key:String = String(rootPosition + relativePositions[i].position);
            // to do >> Not hardcoded to 'Pig'
            if (findSprite(key) != null && GamePiece(targetSprite).color == GamePiece(findSprite(key)).color && 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 < 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 < 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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:20:29+00:00Added an answer on June 10, 2026 at 7:20 pm

    This is actually what I wanted. A way to collapse WITHOUT iterating over the entire board. This way I JUST loop through the items that have been removed.

    protected function _handleGamePiece_CLICK(event:MouseEvent):void
        {
            for ( var j:int = 0 ; j < matches.length ; j++ ) {
                var oldSprite:Sprite = matches[j];
                moveAllPiecesDown(oldSprite);
                view.removeChild(oldSprite);
                oldSprite = null;
            }
            matches.splice(0);
        }
    
    private function moveAllPiecesDown(oldSprite:Sprite):void
        {
            var piecesAbove:int = GamePiece(oldSprite).positionRow;
            var index:int = int(oldSprite.name);
    
            for( var i:int = 0; i < piecesAbove; i ++ ) {
                var spriteAbove:Sprite = Sprite(view.getChildByName(String(index-(1+i))));
                if(spriteAbove) {
                    spriteAbove.y = spriteAbove.y + spriteAbove.height + 1;
                    spriteAbove.name = String(Number(spriteAbove.name)+1);
                    GamePiece(spriteAbove).textField.text = spriteAbove.name;
                    delete dictionary[spriteAbove.name];
                    dictionary[spriteAbove.name] = spriteAbove;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried writing a program in Java using regex to match a pattern and
I am writing a simple search engine for my database using mysqli and prepared
I'm writing a simple JavaScript 3D engine from scratch using Canvas and box like
I'm writing a simple 2D game engine using the HTML5 canvas. I've come to
I am writing a small windows script in javascript/jscript for finding a match for
I was thinking... when im writing (js) var t=1234ABC4321.match(/.*(ABC).*/) it returns : [1234ABC4321, ABC]
I'm writing a Ruby C Extension where I'm using math.h . It's being compiled
Writing a python program, and I came up with this error while using the
I'm writing some regex to match lines which contain numeric elements padded with spaces,
I want to match \Q and \E in a Java regex. I am writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.