A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Drawing lines in Flash MX

  1. #1
    Senior Member
    Join Date
    Dec 2000
    Posts
    126
    What an easy subject! The question is a little harder...

    I have 2 instances of a movie clip... box1 and box2

    You can drag and drop both of them.

    Somehow I want to have a line connecting their centers at all times... so I need to keep that line going dynamically.

    How do I accomplish this? I wish there was a way to "tie" one line to another...

    Thanx in advance!!

    -Dan

  2. #2
    Junior Member
    Join Date
    Dec 2001
    Posts
    14
    Use localToGlobal() to get the object center for both MCs and draw a line from the two points. Since they are drag and drop have it redraw from onClipEvent(mouseMove)for example.


    Examples are in the localToGlobal() and globalToLocal() reference documentation and once you have the global x and y for both boxes you can draw a line between them.
    ...
    MC.moveTo(x,y);
    MC.LineTo(x,y);
    ...

  3. #3
    Senior Member
    Join Date
    Dec 2000
    Posts
    126
    Do I have to clear the line each time the clip is dragged? If so, won't that look choppy?

  4. #4
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    Okay SuperMario, time to kick some goomba a$$.

    I want you to make a movieclip for me, do it in this manner please:

    1) Draw a straight, horizontal line, doesn't matter the distance at this point.. just long enough to see it.

    2) Make the thickness hairline

    3) rotate the line 45 degrees, exactly

    4) select the line and in the info window change the width and height to 100

    5) select the line and hit f8 to make it a movieclip symbol, name it "line" or whatever, the registration mark should be at the upper left corner.

    6) you should now see a diagonal line going down from left to right in a a 100 pixel by 100 pixel box.

    got that?

    Now drop an instance of "line" on the stage and this is the code we are going put on it...

    Code:
    onClipEvent (enterFrame) {
    	this._x=_root.box1._x;
    	this._y=_root.box1._y;
    	getDelta();
    	this._xscale=deltaX;
    	this._yscale=deltaY;
    }
    onClipEvent (load) {
    	function getDelta(){
    		deltaX= _parent.box2._x-_parent.box1._x;
    		deltaY= _parent.box2._y-_parent.box1._y;
    	}
    }
    How this works is that the "line" movieclip attachs itself to box1. Then it determines what the distance is between to two clips position (deltaX and deltaY). It then uses that information to scale the "line" to box2's coordinates. Because we have it onEnterFrame(), if you were to, say, move the location of one of those boxes, the line would stay connected. The other trick to it is how we constructed that "line" in the first place.

    THis example requires that the "line" movieclip is placed on the stage and the code placed on the "line". HOwever, it is also possible to *attach* the line movieclip and add the functionality to at run time.

    you dig?

    Antibody, stomper of Goombas. ;p

  5. #5
    Senior Member
    Join Date
    Dec 2000
    Posts
    126
    Can I duplicate these actions with the line clip?

    Thx,
    -Dan

  6. #6
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    Could you be a little more specific please? You need to define "duplicate these actions" for me. What actions are you referring too, the code bits, or the actions that I gave you to do?

    Thanks.


  7. #7
    Senior Member
    Join Date
    Dec 2000
    Posts
    126
    Sorry about that, I was tired. In fact, I still am...

    But I gave it a try and it has worked Just the onClipEvents needed to be duplicated as the baseline clip was duplicated. I read somewhere that that won't usually work but it did.

    Thanks dude, you're the shizz.

  8. #8
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    Okay, that is what I thought you were talking about but I wanted to make sure.

    If you have the clip onstage with onClipEvents and you duplicate the clip then all instances will have the onClipEvents.

    If you were using .attachMovieClip method then you would have to add the onClipEvents at runtime using the

    attachedMovieInstanceName.onEnterFrame = function (){
    //add code bits here
    }:

    type functionality.

    You're welcome SuperMario. My pleasure really. Anything to help you save the princess bro, ya know? ;p





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center