lineTo无法正常工作

时间:2011-05-17 21:42:51

标签: actionscript-3 flash-cs5

我遇到了我的代码或其他东西的问题......我正在使用lineTo从一个对象坐标到另一个对象坐标,但是没有第二个对象是行的东西总是在任何地方随机方向在左下角,我被卡住了。

这是代码:

        var spr:Shape = new Shape();
        spr.graphics.clear();
        spr.graphics.lineStyle(2,0xffffff);
        spr.x = latM[1].x;
        spr.y = latM[1].y;
        spr.graphics.lineTo(latM[0].x,latM[0].y);
        trace("latM[0].x = "+latM[0].x+"\tlatM[0].y = "+latM[0].y+
              "\nlatM[1].x = "+latM[1].x+"\tlatM[1].y = "+latM[1].y);
        spr.graphics.lineTo(latM[0].x,latM[0].y);
        addChild(spr);
经过几次尝试后,我发现所有线条都是[错误地写错了]朝左下方TT_TT ..

2 个答案:

答案 0 :(得分:1)

我假设latM [1]和latM [0]是你试图在两者之间画线的两种形状。如果是这种情况,你是否注意到你有两条线路要到同一点?

你需要的是什么。

spr.graphics.moveTo(latM[0].x, latM[0].y);
spr.graphics.lineTo(latM[1].x, latM[1].y);

这是一个小型原型,向您展示它是如何工作的。 (这不是超级固体代码,它是一个快速而肮脏的原型。)

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

    public class Main extends Sprite
    {
        private var obj1:Sprite = new Sprite();
        private var obj2:Sprite = new Sprite();
        private var lineSprite:Sprite = new Sprite();

        // for testing your line.
        // we don't really need it for this prototype however it
        // is being used since this is how your accessing your Objects.
        private var latM:Array = [];

        public function Main() 
        {
            addEventListener(Event.ADDED_TO_STAGE, initMain);
        }

        private function initMain(e:Event):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, initMain);

            obj1.graphics.lineStyle(1, 0);
            obj1.graphics.beginFill(0xccccff);
            obj1.graphics.drawCircle(0, 0, 20);
            obj1.graphics.endFill();
            obj1.x = 100;
            obj1.y = 100;

            obj2.graphics.lineStyle(1, 0);
            obj2.graphics.beginFill(0xffcccc);
            obj2.graphics.drawCircle(0, 0, 20);
            obj2.graphics.endFill();
            obj2.x = 400;
            obj2.y = 200;

            // for testing your line.
            latM.push(obj1, obj2);

            addChild(obj1);
            addChild(obj2);
            addChild(lineSprite);

            addEventListener(Event.ENTER_FRAME, handleEnterFrame);
        }

        private function handleEnterFrame(e:Event):void 
        {
            // this will clear and redraw the line between the two sprites
            // every frame and thus always be up to date.
            lineSprite.graphics.clear();
            lineSprite.graphics.lineStyle(2, 0xff0000);
            lineSprite.graphics.moveTo(latM[0].x, latM[0].y);
            lineSprite.graphics.lineTo(latM[1].x, latM[1].y);

            //obj1.x++; // uncomment this line and you can watch it move and keep the line perfect.
        }

    }

}

答案 1 :(得分:0)

你的意思是向左下方倾斜?
你只能用lineTo绘制一条直线 “lineTo”仅通过其参数从当前点到设定点 moveTo函数将移动该点而不绘图 以下代码将绘制一个100 X 100的框

var spr:Shape = new Shape();
spr.graphics.clear();
spr.graphics.lineStyle(2,0xff00ff);
spr.graphics.moveTo(0,0);
spr.graphics.lineTo(0,100);
spr.graphics.lineTo(100,100);
spr.graphics.lineTo(100,0);
spr.graphics.lineTo(0,0);
addChild(spr);