0

我做了一堂课

import flash.display.BitmapData;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;

class com.classes.CollisionDetection {
    static public function checkForCollision(p_clip1:MovieClip,p_clip2:MovieClip,p_alphaTolerance:Number):Rectangle {
        p_clip1.lShots.text = "D";
        // set up default params:
        if (p_alphaTolerance == undefined) { p_alphaTolerance = 255; }

        // get bounds:
        var bounds1:Object = p_clip1.getBounds(_root);
        var bounds2:Object = p_clip2.getBounds(_root);

        // rule out anything that we know can't collide:
        if (((bounds1.xMax < bounds2.xMin) || (bounds2.xMax < bounds1.xMin)) || ((bounds1.yMax < bounds2.yMin) || (bounds2.yMax < bounds1.yMin)) ) {
            return null;
        }

        // determine test area boundaries:
        var bounds:Object = {};
        bounds.xMin = Math.max(bounds1.xMin,bounds2.xMin);
        bounds.xMax = Math.min(bounds1.xMax,bounds2.xMax);
        bounds.yMin = Math.max(bounds1.yMin,bounds2.yMin);
        bounds.yMax = Math.min(bounds1.yMax,bounds2.yMax);

        // set up the image to use:
        var img:BitmapData = new BitmapData(bounds.xMax-bounds.xMin,bounds.yMax-bounds.yMin,false);

        // draw in the first image:
        var mat:Matrix = p_clip1.transform.concatenatedMatrix;
        mat.tx -= bounds.xMin;
        mat.ty -= bounds.yMin;
        img.draw(p_clip1,mat, new ColorTransform(1,1,1,1,255,-255,-255,p_alphaTolerance));

        // overlay the second image:
        mat = p_clip2.transform.concatenatedMatrix;
        mat.tx -= bounds.xMin;
        mat.ty -= bounds.yMin;
        img.draw(p_clip2,mat, new ColorTransform(1,1,1,1,255,255,255,p_alphaTolerance),"difference");

        // find the intersection:
        var intersection:Rectangle = img.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF);

        // if there is no intersection, return null:
        if (intersection.width == 0) { return null; }

        // adjust the intersection to account for the bounds:
        intersection.x += bounds.xMin;
        intersection.y += bounds.yMin;

        return intersection;
    }


}

在 SWF 中我写

  import com.classes.CollisionDetection;

    function checkCollision(vMc1, vMc2, vT) {
        // check for collisions:
        var collisionRect:Rectangle = CollisionDetection.checkForCollision(vMc1, vMc2, vT);

        if (collisionRect) {
            return true;
        }
    }

它在本地工作没问题,但不会在线工作。有任何想法吗。我玩过类路径等......但没有运气。:( 似乎它在显示测试“D”时工作,但 HIT 没有工作......所以不确定是什么让代码在本地工作而不是在互联网上工作。有什么想法吗?

4

1 回答 1

1

这可能是由浏览器中的 Flash 缩放引起的。基于形状的碰撞检测中使用的所有坐标都基于您第一次制作电影时的舞台大小,因此在缩放时事情会变得一团糟。

一个解决方案是创建一个不可见的参考矩形,当您的电影进行缩放时。我们只需要在您的原始代码中添加一些东西。我不能为此归功于 - Dave Pagurek 由 Grant Skinner 创建了这个对原件的修改。

import flash.geom.Rectangle;
import flash.display.BitmapData;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;

checkForCollision = function(p_clip1:MovieClip, p_clip2:MovieClip, p_alphaTolerance:Number):Rectangle  {

    // our magic rectangle:
    if (stageMaskRect) {
        // if this rectangle exists, scale it:
        stageMaskRect._width = Stage.width;
        stageMaskRect._height = Stage.height;
        stageMaskRect._x = 0;
        stageMaskRect._y = 0;
    } else {
        // if not, create it:
        createEmptyMovieClip("stageMaskRect",0);
        stageMaskRect.beginFill(0xFF0000,0);
        stageMaskRect.moveTo(0,0);
        stageMaskRect.lineTo(Stage.width,0);
        stageMaskRect.lineTo(Stage.width,Stage.height);
        stageMaskRect.lineTo(0,Stage.height);
        stageMaskRect.endFill();
        stageMaskRect._x = 0;
        stageMaskRect._y = 0;
    }

    // set up default params:
    if (p_alphaTolerance == undefined) {
        p_alphaTolerance = 255;
    }

    // get bounds:   
    var bounds1:Object = p_clip1.getBounds(_root);
    var bounds2:Object = p_clip2.getBounds(_root);
    // rule out anything that we know can't collide:
    if (((bounds1.xMax<bounds2.xMin) || (bounds2.xMax<bounds1.xMin)) || ((bounds1.yMax<bounds2.yMin) || (bounds2.yMax<bounds1.yMin))) {
        return null;
    }

    // determine test area boundaries:   
    var bounds:Object = {};
    bounds.xMin = Math.max(bounds1.xMin, bounds2.xMin);
    bounds.xMax = Math.min(bounds1.xMax, bounds2.xMax);
    bounds.yMin = Math.max(bounds1.yMin, bounds2.yMin);
    bounds.yMax = Math.min(bounds1.yMax, bounds2.yMax);

    // set up the image to use: Here's our magic rectangle:
    var img:BitmapData = new BitmapData(stageMaskRect._width, stageMaskRect._height, false);

    // draw in the first image:
    var mat:Matrix = p_clip1.transform.concatenatedMatrix;
    mat.tx -= bounds.xMin;
    mat.ty -= bounds.yMin;
    img.draw(p_clip1,mat,new ColorTransform(1, 1, 1, 1, 255, -255, -255, p_alphaTolerance));

    // overlay the second image:
    mat = p_clip2.transform.concatenatedMatrix;
    mat.tx -= bounds.xMin;
    mat.ty -= bounds.yMin;
    img.draw(p_clip2,mat,new ColorTransform(1, 1, 1, 1, 255, 255, 255, p_alphaTolerance),"difference");

    // find the intersection:
    var intersection:Rectangle = img.getColorBoundsRect(0xFFFFFFFF, 0xFF00FFFF);

    // if there is no intersection, return null:
    if (intersection.width == 0) {
        return null;
    }

    // adjust the intersection to account for the bounds:   
    intersection.x += bounds.xMin;
    intersection.y += bounds.yMin;
    return intersection;
};

希望这会让您开始并避免其他人因为我试图找到解决方案而感到沮丧!

于 2014-02-04T14:58:41.670 回答