9 Replies Latest reply on Aug 27, 2010 5:35 AM by kglad

    Joining two points

    dalcde Level 1

      I want to ask how do you join two points with a line using actionscript? The points would be constantly moving.

        • 1. Re: Joining two points
          kglad Adobe Community Professional & MVP

          you can use joinF() to draw a line from x1,y1 to x2,y2:

           

          var canvas:MovieClip=this.createEmptyMovieClip("canvas",this.getNextHighestDepth());

           

          function joinF(x1:Number,y1:Number,x2:Number,y2:Number):Void{

          with(canvas){

          clear();

          lineStyle(0,0xff0000);

          moveTo(x1,y1);

          lineTo(x2,y2);

          }

          }

          • 2. Re: Joining two points
            dalcde Level 1

            Could you briefly explain the code?

            • 3. Re: Joining two points
              kglad Adobe Community Professional & MVP

              :

               


              // the as2 drawing api needs a movieclip to apply the draw methods.  so create one:

               

              var canvas:MovieClip=this.createEmptyMovieClip("canvas",this.getNextHighe stDepth());

               

               

               

              function joinF(x1:Number,y1:Number,x2:Number,y2:Number):Void{

              with(canvas){

              // this clears anything previously added to canvas using the drawing api

              // ie, if joinF() is going to be called repeatedly, you need to use the clear() method

              // or all the lines will appear.  try commenting it out and calling joinF() repeatedly

              clear();

              // this establishes a "style" for the line:  0 thickness, red color

              // check the movieclip help files for the other parameters

              lineStyle(0,0xff0000);

              // start drawing at x1,y1

              moveTo(x1,y1);

              // draw a line to x2,y2

              lineTo(x2,y2);

              }

              }

              1 person found this helpful
              • 4. Re: Joining two points
                dalcde Level 1

                Actually what I don't understand is the "with(canvas)" part. What does it mean? Does it mean all the "this" within the "with" part refers to the canvas? Last question: I am actually making a 3-D graphic and I wan to make a button that would rotate  a cube.The problem is that I want the cube to rotate as long as the mouse is down, i.e. I don't have to keep pressing the button to rotate it several times. I only have to hold the mouse down.

                • 5. Re: Joining two points
                  kglad Adobe Community Professional & MVP

                  there is no "this" in that with(){} statement.  the with-statement is shorthand.  the following two snippets are the same:

                   

                  with(canvas){

                  clear();

                  lineStyle(0,0xff0000);

                  moveTo(x1,y1);

                  lineTo(x2,y2);

                  }

                   

                  // and


                  canvas.clear();

                  canvas.lineStyle(0,0xff0000);

                  canvas.moveTo(x1,y1);

                  canvase.lineTo(x2,y2);

                  1 person found this helpful
                  • 6. Re: Joining two points
                    dalcde Level 1

                    Thanks a lot, but could you help me with the last question at my previous post (I just edited it so you might have not seen it). This is my first attempt to 3-D graphics and I am surprised that most of the problems are not because of the 3-D design!

                    • 7. Re: Joining two points
                      kglad Adobe Community Professional & MVP

                      i don't see a question, but you should start a loop (like onEnterFrame or setInterval) when your button is pressed (and clear the loop when the button is released).  in that loop, you should call the joinF().

                      • 8. Re: Joining two points
                        dalcde Level 1

                        Thanks. I haven't thought of that.

                        • 9. Re: Joining two points
                          kglad Adobe Community Professional & MVP

                          you're welcome.