0

Hi I'm noticing that the following code is producing a noticeable chunk of lag when it is incorporated.

    public function impassable_collisions():void { //prevent player from moving through impassable mcs

        for each(var mc:MovieClip in impassable) {

        var bitmap = createBitmapClone(mc); //return clone of bitmap of mc, accounting for transformations

        var bounds:Rectangle = player.getRect(bitmap);
        var playerCenterX:Number = bounds.left + bounds.width * .5;
        var playerCenterY:Number = bounds.top + bounds.height * .5;

        //Test for a pixel perfect collision against a shape based on a point and radius, 
        //returns the angle of the hit or NaN if no hit.
        var hitAngle:Number = hitTestPointAngle(bitmap, playerCenterX, playerCenterY, player.height/2);

        if(!isNaN(hitAngle)){
            // move player away from object
            player.x -= int(Math.cos(hitAngle) * player.speed*timeDiff);
            player.y -= int(Math.sin(hitAngle) * player.speed*timeDiff);
        }
    }
}

So this function is called from another function which is called from an enterFrame listener. I'm looping through an array containing perhaps between 5-30 movieclips at any given time, the given mc will then be cloned to a bitmap and that bitmap will be used to test for collisions between itself and another bitmap (the "player"). I'm assuming that something rather inefficient is taking place and thus producing lag - more choppy-like movements when the "player" is moved. Does anyone have an idea what the problem is/solutions to it?

Sorry for the rather vague question title

thanks

4

1 回答 1

1

Pixel Perfect 碰撞检测成本很高。我不能说这是唯一的问题,但我可以告诉你它在处理方面很昂贵。

在这个循环中每次迭代创建一个新的位图也很昂贵。

您可以尝试的一件事是在进行更深入的检查之前消除对象。例如,如果对象不在特定距离内,则非常快速的距离检查可以消除测试对象的需要。

if (distance <= threshold)
{
    // create the bitmap for the check
    // do the expensive pixel perfect collision check
}

您的目标是消除明显距离不够近而无法碰撞的物体。

于 2013-04-19T23:16:35.973 回答