Search code examples
actionscript-3collision-detectionhittest

AS3 - Is it possible to test objects in the same array against each other?


I am making this project that allows the player to push objects. I can test the player against the object array just fine using the "for" loop and I can move the objects around.

What I want to do now is test the objects against each other and have them move each other as well. Like, I want to push a block with the player and then push another block with the block I'm already pushing.

The blocks are all pushed into their own arrays, so how could I test them against each other?


Solution

  • Sure, you can place everything in the same array and use two for loops:

    for each(var a:Entity in array)
    {
        for each(var b:Entity in array)
        {
            // Objects can't collide with themselves.
            if(a == b) continue;
    
    
            // Check if a and b collide and do something.
            //
        }
    }
    

    This example code assumes that Entity is a base class for your objects that can touch each other; all of which are listed within array.