Actionscript 3 (Actionscript 3)墙壁与播放器之间像素级完美碰撞检测?

Actionscript 3 (Actionscript 3)墙壁与播放器之间像素级完美碰撞检测?,actionscript-3,flash,Actionscript 3,Flash,我正在尝试制作一个flash游戏,其中玩家和墙壁之间有碰撞检测。但是,当我尝试使用Wall11.hitTestPoint()时,碰撞检测无法达到完美。然后,我决定使用位图,但很难编写代码,因为墙的形状不规则(不是正方形、矩形、圆形或任何规则形状)。是否有改进与墙壁碰撞检测的方法 function checkCollision(_debug:Boolean = false):Boolean { var bmd1:BitmapData = new BitmapD

我正在尝试制作一个flash游戏,其中玩家和墙壁之间有碰撞检测。但是,当我尝试使用Wall11.hitTestPoint()时,碰撞检测无法达到完美。然后,我决定使用位图,但很难编写代码,因为墙的形状不规则(不是正方形、矩形、圆形或任何规则形状)。是否有改进与墙壁碰撞检测的方法

function checkCollision(_debug:Boolean = false):Boolean {           
        var bmd1:BitmapData = new BitmapData(Wall11.width, Wall11.height, true, 0);
        var bmd2:BitmapData = new BitmapData(LevelOnePlayer.width, LevelOnePlayer.height, true, 0);

        bmd1.draw(Wall11);
        bmd2.draw(LevelOnePlayer);

        if (_debug) {
            var bmp:Bitmap = new Bitmap(bmd1);
            bmp.x = Wall11.x;
            bmp.y = Wall11.y;
            addChild(bmp);

            var bmp2:Bitmap = new Bitmap(bmd2);
            bmp2.x = LevelOnePlayer.x;
            bmp2.y = LevelOnePlayer.y;
            addChild(bmp2);
        }
        if(bmd1.hitTest(new Point(Wall11.x, Wall11.y), 255, bmd2, new Point(LevelOnePlayer.x, LevelOnePlayer.y), 255))
        return true;
        if (!_debug) {
            bmd1.dispose();
            bmd2.dispose();
        }
        return false;
    }    

这些是hitTestPoint的基础知识

package 
{
    import flash.geom.Point;

    import flash.events.Event;

    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;

    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;

    public class HitTest extends Sprite
    {
        private var textArea:TextField;
        private var Circle:Shape;
        private var Box:Shape;

        public function HitTest()
        {
            if (stage) onStage();
            else addEventListener(Event.ADDED_TO_STAGE, onStage);
        }

        private function onStage(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, onStage);

            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.showDefaultContextMenu = false;
            stage.align = StageAlign.TOP_LEFT;
            stage.stageFocusRect = false;

            addShapes();
            addLabel();
            onFrame();

            // Call it every frame to keep things updated.
            addEventListener(Event.ENTER_FRAME, onFrame);
        }

        private function onFrame(e:Event = null):void
        {
            // Place graphics to the center of the stage.
            x = stage.stageWidth >> 1;
            y = stage.stageHeight >> 1;

            // Lets detect collision with the circle.
            var aPoint:Point = new Point();

            // Take local mouse coordinates within the target shape.
            aPoint.x = Circle.mouseX;
            aPoint.y = Circle.mouseY;

            // Convert them into the root coordinates - it is the ONLY correct way.
            // Comment the next 2 lines to see how local coordinates fail to work.
            aPoint = Circle.localToGlobal(aPoint);
            aPoint = root.globalToLocal(aPoint);

            // Hit test the point against shape.
            // Set the last parameter to false to see hitTest against the shape bounding box.
            var aHit:Boolean = Circle.hitTestPoint(aPoint.x, aPoint.y, true);

            textArea.text = aHit? "! HIT !": "NO HIT";
        }

        private function addShapes():void
        {
            Circle = new Shape();
            Circle.graphics.lineStyle(2, 0x000000, 1);
            Circle.graphics.beginFill(0xCC99FF, 1);
            Circle.graphics.drawCircle(0, 0, 50);
            Circle.graphics.endFill();

            Box = new Shape();
            Box.graphics.lineStyle(0, 0xCCCCCC, 1);
            Box.graphics.drawRect(-50, -50, 100, 100);

            addChild(Box);
            addChild(Circle);
        }

        private function addLabel():void
        {
            textArea = new TextField();

            textArea.x = 10;
            textArea.y = 10;
            textArea.width = 70;
            textArea.height = 20;

            textArea.border = true;
            textArea.wordWrap = false;
            textArea.multiline = true;
            textArea.selectable = true;
            textArea.background = true;
            textArea.mouseEnabled = false;

            var aFormat:TextFormat;

            aFormat = textArea.getTextFormat();
            aFormat.font = "_typewriter";
            aFormat.size = 12;
            aFormat.bold = true;
            aFormat.align = TextFormatAlign.CENTER;

            textArea.setTextFormat(aFormat);
            textArea.defaultTextFormat = aFormat;

            stage.addChild(textArea);
            textArea.text = "NO HIT";
        }
    }
}

请解释为什么hitTestPoint(最后一个参数shapeFlag=true)不适用于您?无论我将该点设置为什么,当玩家离墙太近或太远时,墙总是检测到碰撞。我不知道如何将碰撞设置为我希望碰撞点的位置。再次,排除这种可能性。你使用形状和形状标志吗?另外,您是使用阶段坐标还是局部坐标?对不起,什么是形状和形状标志?我也不知道舞台坐标和局部坐标之间的区别。你能给我发一个链接/代码吗?我在下面贴了一个答案,这样你就可以开始了。很抱歉,我已经一个月没有回复了。无论如何,谢谢你的帮助。但我不清楚Circle.mouseX是什么意思?@GaryLuKOTH Circle坐标系中的本地鼠标坐标。请阅读和,以便更好地理解。谢谢。现在我了解了你的代码是如何工作的。现在,我只需要知道如何在不同形状之间找到碰撞检测,而不仅仅是鼠标。@GaryLuKOTH,方法完全相同,但您可以将某个英雄相关点(例如,其中心)作为原点,而不是鼠标。谢谢。现在,我得到了它们互相撞击时要检测的形状。现在,我应该编写什么代码,使对象不能重叠?比如,我想让墙挡住广场。我该怎么做?