跟随movieclip中的另一个实例

时间:2011-08-03 06:53:30

标签: flash actionscript-3

我在制作一条线以跟随另一个实例(十字准线)时遇到问题,我的线路没有跟随我的十字准线,这不能因为我将lineTo设置为我的十字准线的x和y位置

swf在这里http://megaswf.com/filelinks/1156538

我执行这些行的代码就是这些。

_cross是十字准线实例,而rodhit是我在钓鱼竿的“尖端”上放置的s符号的实例,因此我没有包含一个动作。

addEventListener(Event.ENTER_FRAME, crossLoop);

public function crossLoop(e:Event):void
        {

            //calculations, distance, angle etc
            if (_cross != null)
            {
            var a:Number = _cross.x- x;
            var b:Number = _cross.y - y;
            rodhit.graphics.lineTo(_cross.x, _cross.y);
            var angRad:Number = Math.atan2(b, a);
            var angDeg:Number = (angRad * 180 / Math.PI);
            //trace(angDeg );

            rotation = (angDeg);
            if (angDeg > -10)
                rotation = -10;
            if (angDeg < -170)
                rotation = -170


            }
        }

1 个答案:

答案 0 :(得分:1)

以下是一个如何执行此操作的示例:http://wonderfl.net/c/6K52

package 
{
    import flash.display.Shape;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        private var _cross:Sprite;
        private var _line:Shape;
        public function FlashTest() {
            _cross = new Sprite();
            _cross.graphics.beginFill(0xFF0000, 1);
            _cross.graphics.drawCircle(0, 0, 20);
            addChild(_cross);

            _line = new Shape();
            _line.x = 200;
            _line.y = 30;
            addChild(_line);

            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onMouseMove(evt:MouseEvent):void
        {
            _cross.x = stage.mouseX;
            _cross.y = stage.mouseY;
        }

        private function onEnterFrame(evt:Event):void
        {
            _line.graphics.clear();
            _line.graphics.lineStyle(2, 0x000000, 1);
            _line.graphics.lineTo(_cross.x - _line.x, _cross.y - _line.y);
        }
    }
}