在 actionscript 3 中另一个动画片段与其相交的点处将线弯曲 90 度

bend line 90 degrees at a point where another movieclip intersects with it in actionscript 3

本质上,我是在 Flash CS6 中制作一个将光源投射到屏幕上的游戏。你会得到一面可以拖来拖去的镜子。然而,当镜子接触到光线时,光线应该会被反射回来,并在光线到达镜子后最终偏移 90 度。
我没有足够的声誉来 post 图片,但这里有一个 link 对问题的解释:http://raphaelhennessy.com/misc/explanation.png
如果你能帮我解决这个问题,我会很高兴。

提前致谢,
-拉夫

这是一个非常 快速而肮脏的例子,可以让球滚动起来。这段代码有很多问题,所以我建议你只把它作为一个起点。详细信息在代码中进行了注释。

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class MirrorTest extends Sprite 
    {

        private var _mirror:Sprite;
        private var _line:Sprite;

        public function MirrorTest() 
        {
            super();

            // create the line clip
            _line = addChild(new Sprite()) as Sprite;
            _line.graphics.clear();
            _line.graphics.lineStyle(3, 0xFF0000);
            _line.graphics.moveTo(0, 0);
            _line.graphics.lineTo(500, 0); // starting it off at an arbitrary width
            _line.y = 100; // positioning line so that we can see when it bends upwards

            // create the mirror clip
            _mirror = addChild(new Sprite()) as Sprite;

            // draw a square and rotate it so we have a diamond shape
            _mirror.graphics.beginFill(0);
            _mirror.graphics.drawRect(-20, -20, 40, 40);
            _mirror.graphics.endFill();
            _mirror.rotation = 45;
            _mirror.x = _mirror.y = 200; // position the mirror away from the line

            // add even listeners to trigger dragging/dropping
            _mirror.addEventListener(MouseEvent.MOUSE_DOWN, dragMirror);
            _mirror.addEventListener(MouseEvent.MOUSE_UP, dropMirror);

        }

        private function dragMirror($event:MouseEvent):void 
        {
            // start dragging the mirror. 
            _mirror.startDrag(true);
            // add the ENTER_FRAME listener so that we can check for colision as the mirror is being moved around
            addEventListener(Event.ENTER_FRAME, onTick);
        }

        private function dropMirror($event:MouseEvent):void 
        {
            // stop dragging the mirror
            _mirror.stopDrag();
            // remove the ENTER_FRAME listener so we don't waste cycles checking for collision when the mirror is not being moved around
            removeEventListener(Event.ENTER_FRAME, onTick);
        }

        private function onTick($event:Event):void 
        {
            // check to see if the mirror has collided with the line
            if (_mirror.hitTestObject(_line))
            {
                // if so, redraw the line as a right-angle, using the mirror's position as the "collision point"
                _line.graphics.clear();
                _line.graphics.lineStyle(3, 0xFF0000);
                _line.graphics.moveTo(0, 0);
                _line.graphics.lineTo(_mirror.x - _mirror.width * .5, 0);
                _line.graphics.lineTo(_mirror.x - _mirror.width * .5, -100);
            }
            else
            {
                // if not, redraw the original line
                _line.graphics.clear();
                _line.graphics.lineStyle(3, 0xFF0000);
                _line.graphics.moveTo(0, 0);
                _line.graphics.lineTo(500, 0);
            }

        }

    }

}