19 Replies Latest reply on Nov 14, 2006 1:29 AM by arunbe

    Dynamic line drawing...

    DIY_Lobotomy Level 1
      Hello. I have a situation that I don't know how to accomplish with actionScript (although I know it's possible). Can anyone help out with some code or a tutorial online somewhere? Here's the question...

      I have a number of MCs on the stage. I want it so that when a user clicks on any one MC and then on any other MC, then a line will be drawn between the two ("connecting" them, basically)...

      Thanks!...
        • 1. Re: Dynamic line drawing...
          kglad Adobe Community Professional & MVP
          use the movieclip lineStyle(), moveTo(), lineTo() and curveTo() methods.
          • 2. Re: Dynamic line drawing...
            arunbe Level 1
            hi
            u can use this code.
            • 3. Re: Dynamic line drawing...
              DIY_Lobotomy Level 1
              Thanks for the code, arunbe!...

              Question, though (just so I can understand exactly what you did) - what does this line mean?:

              sqr2.onRelease = sqr1.onRelease=function () {

              I've never seen that sort of syntax before. Thanks!...

              --Matt
              • 4. Re: Dynamic line drawing...
                Bob_Robertson Level 1
                It means the same thing that x=y=5 means: that x is the result of y=5, which is 5.

                In effect, it's a quick and easy method of assigning the same value to two different properties without having to retype it.
                • 5. Re: Dynamic line drawing...
                  DIY_Lobotomy Level 1
                  Ahhh, okay - I see...

                  In playing with that code now, I actually have a couple other questions....

                  First, I don't see how the "drawRectangle" function comes into play. I thought it seemed to be doing nothing, so I commented out the function (as well as the function call), and when I tested the movie it still worked fine. Am I missing something?...

                  Also, is there a way to clear (or "erase") the line that was just drawn?...

                  Thanks again so much for all the help!...
                  • 6. Re: Dynamic line drawing...
                    arunbe Level 1
                    yes,
                    you can use clear function to clease the particular movie clip or stag.
                    movieclip.clear();
                    • 7. Dynamic line drawing...
                      DIY_Lobotomy Level 1
                      quote:

                      Originally posted by: arunbe
                      yes,
                      you can use clear function to clease the particular movie clip or stag.
                      movieclip.clear();


                      Not sure how that would work. I put a mc on the screen called "clear_mc", and have the following code (which is doing nothing, so I'm probably doing something wrong):

                      clear_mc.onRelease = function() {
                      sqr1.clear();
                      sqr2.clear();
                      }

                      Any ideas?...

                      • 8. Re: Dynamic line drawing...
                        arunbe Level 1
                        use this code.
                        i made another movie clip in the top to delete the drawn lines.
                        • 9. Re: Dynamic line drawing...
                          DIY_Lobotomy Level 1
                          Okay - getting closer! Now here are my questions...

                          Again, like before, I don't understand what any of the code regarding the "drawRectangle" function does? Commented out, it still works exactly the same. I just compeltely eliminated it all together. Why have it in there?...

                          Secondly (and this is kinda important for what I'm doing), I have four MCs on stage (sqr1, sqr2, sqr3, sqr4). So, when the _root.clear() is called, it clears ALL lines drawn. I need it to only clear CERTAIN lines. So, if I've drawn a line between sqr1 and sqr2, and then another line between sqr3 and sqr4, how do I ONLY clear ONE of those two lines?...

                          Thanks again - I'm learning a lot from this. Hope I'm not being a pain!...
                          • 10. Re: Dynamic line drawing...
                            Glazer
                            Hi,

                            "drawRectangle" is a function which basically "draws" a mc, but it doesn't seem that you need this function, it sounds as if you already have mc's on the stage, you just need to be able to draw a line between them?
                            Then you have a mc "clear_mc" which will remove the line, correct?
                            • 11. Re: Dynamic line drawing...
                              DIY_Lobotomy Level 1
                              quote:

                              Originally posted by: Glazer
                              Then you have a mc "clear_mc" which will remove the line, correct?

                              Yes - I have a "clear_mc" MC which clears ALL the lines (as I said, I altered the example to have MULTIPLE clips on the stage, so there are MULTIPLE lines that could be drawn). What I want to do is just clear SPECIFIC lines, as opposed to the _root.clear() command that clears ALL the lines. Here's the code I have that currently works to draw lines in between four MCs that have have drawn on the stage using Flash's drawing tools (not dynamic):
                              • 12. Re: Dynamic line drawing...
                                Bob_Robertson Level 1
                                If you have a reference to the movie clip that you want to delete, you can use the global removeMovieClip function. If you don't have a reference, you can use eval("movieName") to get one: _root.removeMovieClip(eval(movieName)). If you want to extend this for algorithmic use, you can use createEmptyMovieClip("clipName"+i,...) and increment i each time you create a new point.

                                As regards the drawRectangle business: when createEmptyMovieClip is called, the clip created has no visible shape. So, drawRectangle gives it one. Practically speaking, it's irrelevant, but it makes the endpoints more visible.
                                • 13. Re: Dynamic line drawing...
                                  Glazer Level 1
                                  ok, try this code...
                                  it may look a little intimidating at first, but basically what it does is:
                                  - add the mc's to an array
                                  - add a function call to each mc (onRelease)
                                  - creates a new "Line" mc (dependant on how many lines there are, eg: Line0, Line1, Line2, etc.)
                                  - adds a function to each line, so that if you click on the line, it disappears
                                  Not exactly what you wanted, but does work, the other option is to create a "clear_mc" for each line that is created.


                                  • 14. Dynamic line drawing...
                                    DIY_Lobotomy Level 1
                                    quote:

                                    Originally posted by: Bob Robertson
                                    If you have a reference to the movie clip that you want to delete, you can use the global removeMovieClip function. If you don't have a reference, you can use eval("movieName") to get one: _root.removeMovieClip(eval(movieName)). If you want to extend this for algorithmic use, you can use createEmptyMovieClip("clipName"+i,...) and increment i each time you create a new point.

                                    I'm not at all sure how to do this. Here's a link to the .swf online. Maybe this will clear up what I'm trying to do. Again, I posted the exact code in my last post that I am using to acheive this. I just want to be able to PICK a line to remove instead of using _root.clear() to clear ALL the lines. Thanks again for any help that can be offered:

                                    http://al-i.anixter.com/TEMP/dynamicLineDraw.html

                                    To be even MORE specific, what I'd like to happen is that if you click one of the MCs that ALREADY has a line shooting out of it, then it will clear the CURRENT line, and re-draw a new one. So, basically, each MC would only be allowed to have ONE line coming out of it. I hope this makes sense. Thanks!...

                                    EDIT: The post above this must've been being posted as I was typing mine. I checked it out, and it works. But I still have the issue of how I would, from a practical standpoint, have the line disappear if the first MC clicked on already has a line coming out of it.

                                    • 15. Re: Dynamic line drawing...
                                      Bob_Robertson Level 1
                                      Is the number of end points static?
                                      • 16. Re: Dynamic line drawing...
                                        DIY_Lobotomy Level 1
                                        quote:

                                        Originally posted by: Bob Robertson
                                        Is the number of end points static?

                                        Yes. There should only be able to be one connection point on each MC. So, I guess if you clicked an MC that didn't have a line on it, but then you clicked the 2nd MC and it already had a line on it, it would just do nothing (like, the 2nd MC wouldn't be clickable, maybe?)...

                                        But if the 1st MC that you click already had a line coming out of it, then that line would just disappear to create the new line.

                                        Hope that makes sense!

                                        • 17. Dynamic line drawing...
                                          Bob_Robertson Level 1
                                          Helpful and illuminating, but not quite what I asked.
                                          I was asking if the number of end points, which you were calling sqr1, sqr2, and the like, was static: do these have to be created dynamically? Is their position going to change, ever?
                                          • 18. Re: Dynamic line drawing...
                                            DIY_Lobotomy Level 1
                                            Ahhhh - sorry. Didn't understand the question...

                                            ...no - they will never be created dynamically, and they will never change position (once they are initially set on the stage).

                                            • 19. Re: Dynamic line drawing...
                                              arunbe Level 1
                                              hi DIY_Lobotomy ,

                                              You can do one thing !
                                              Better you create empty movie clip for all the lines to be drawn and store them all the name in an array.
                                              when you click the clear_btn. just you clear the elements one by one using their name stored in the array....