开发者

Proximity 'Hit tests' from multiple arrays (Action Script 3)

I know this should be possible and Im pretty confident with my code. I have two arrays that are dynamically filled from another function. When two sprites from each array interact they should trigger a function, but at the moment they just glide right past each other.

public function mixGender ():void
{
    for (var gi:int = 0; gi < firstSpriteArray.length; gi++)
    {
        var sprite1 = firstSpriteArray[gi];
        for (var pi:uint = 0; pi < secondSpriteArray.length; pi++)
        {
            var sprite2:pinkSprite = secondSpriteArray[pi];
            var dist = getDistance(sprite1.x,sprite1.y,sprite2.x,sprite2.y);
            if (dist < 28)
            {
                /*and if they are within touc开发者_StackOverflowhing range calls a function.*/
                function ();
            }
        }
    }
}

There must be something obvious Im missing. Any hints?


Give this a whirl:

public function mixGender():void
{
    // Method constants
    var radius:int = 28;

    // Collisions
    for each(var a:Sprite in firstSpriteArray)
    {
        for each(var b:Sprite in secondSpriteArray)
        {
            // Measure distance
            var d:Number = Point.distance(
                new Point(a.x, a.y),
                new Point(b.x, b.y)
            );

            if(d < radius)
            {
                trace('collision');
            }
        }
    }
}

Bit of testing with this code on the timeline:

var firstSpriteArray:Array = [new Sprite()];
var secondSpriteArray:Array = [new Sprite()];

mixGender(); // collision

firstSpriteArray[0].x = 29;
mixGender(); // nothing
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜