开发者

lineTo not working properly

i'm having problems with my code or something... the thing is i'm using lineTo from one objects coordinates to the other's, but no mater where the second object is the line always goes off to a random direction somewhere in the lower left corner and i'm stuck.

here is the code:

        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 = "+开发者_JAVA技巧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);

after a few tries i found out that all lines point [wrote lean by mistake] towards the lower left TT_TT..


I assume latM[1] and latM[0] are the two shapes your trying to draw a line between. If that is case did you notice you have two lineTo going to the same point?

What you need is.

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

Here is a small prototype to show you how it works. (This is not meant to be super solid code it is a quick and dirty prototype.)

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.
        }

    }

}


What do you mean lean towards the lower left?
You can only draw a straight line with lineTo.
"lineTo" only goes from the current point to the point set via its parameters.
The moveTo function will move the point without drawing.
The following code will draw a box 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜