actionscript-3flashdevelopflash-cs3

How to hitTest same Objects in one Array?


I want to create a stacking Game. Where when you tap the screen for instance a block falls down and a new one appears where the other one originally was. Now when the User taps the screen again the same block falls down and if aligned correctly stacks on top of the first one so one and so one. Keep stacking until you miss.

I thought creating an array and pushing each new object to that array would be able to hitTest between each new one etc and have them stack on each other. I realized I don't quite understand how to go about doing this. New instances are created so I got that down. Here is my code so far:

private function engineLogic(e:Event):void 
    {
        stackingHandler();
    }
    
    private function stackingHandler():void 
    {
        for (var i:int = 0; i < aCatArray.length; i++)
        {
            var currentCat:mcCats = aCatArray[i];
            
            //HIT TEST CATS 
            
        }
        trace("NUMBER OF CATS: " + aCatArray.length);
    }
    
    private function onTap(e:MouseEvent):void 
    {
        //Move Down 
        TweenLite.to(cats, 1.0, {y:(stage.stageHeight / 2) + 290, onComplete: addCats}); 
    }
    
    private function addCats():void 
    {
        //Create Instance
        cats = new mcCats();
        //Add Objects
        addChild(cats);
        //Push to Array
        aCatArray.push(cats);
        
    }

I would appreciate any help from you guys. Maybe if you can push me in the right direction. Thank you in advance!


Solution

  • This is how I was able to fix it. Creating a double for loop. Checking if they are equal to each other continue and check for hitTest:

    private function stackingHandler():void 
    {
        for (var i:int = 0; i < aCatArray.length; i++)
        {
            var currentCat:mcCats = aCatArray[i];
            
            for (var j:int = 0; j < aCatArray.length; j++)
            {
                var newCat:mcCats = aCatArray[j];
                
                if (currentCat == newCat) continue;
                
                //Hit Test between Objects
                if (newCat.hitTestObject(currentCat.mcHit) && newCat.bFlag == false)
                {
                    //Stop Moving
                    newCat.stopMoving();
                    trace("HIT");
                    
                    if (highScore == 0)
                    {
                        addCats();
                        trace("ADD CATS 1");
                    }else
                    {
                        TweenLite.delayedCall(0.6, addCats);
                        trace("ADD CATS 2");
                    }
                    
                    //Add Points
                    highScore ++;
                    trace(highScore + " Score");
                    //Set Flag boolean
                    newCat.bFlag = true
                }
            
            }
            
        }
    
    }