sutoL
sutoL

Reputation: 1767

line following another instance in movieclip

i am having trouble making a line to follow another instance(crosshair),my line is not following my crosshair, which cant be because i have set the lineTo to the x and y positions of my crosshair

the swf is here http://megaswf.com/filelinks/1156538 ,

the code where i do the lines are these.

_cross is the cross hair instance, and rodhit is a instance of s symbol that i have put on the "tip" of a fishing rod, therefore i did not include a moveTo.

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


            }
        }

Upvotes: 0

Views: 260

Answers (1)

Philipp Kyeck
Philipp Kyeck

Reputation: 18860

here is an example how you could do this: 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);
        }
    }
}

Upvotes: 1

Related Questions