3 Replies Latest reply on May 16, 2009 7:11 AM by ATIF FAROOQ

    Smart Guides

    Seglespaan Level 1

      Hi,

       


      I'm trying to set up smart guides for my application. The user is able to add objects (children) to a canvas, these can then be dragged and dropped around the canvas. What I want is that when the currently being dragged child is in line with any of the other children a line appears to show the user they are in line.

       


      What I have just now works to a degree, but for some reason the smart guide only shows when the currently being dragged object is inline with the last child to be added to the canvas. i.e.

       


      If I add child1 and child2 to the canvas and drag them around the guide shows up as expected.

       


      If I add child3 to the canvas, child1 and child2 now only line up with child three, no longer with each other.

       


      If I then add child4 to the canvas, child1,2,3 now only line up with child 4.

       


      Any help would be much appreciated.

       

       

       

      Here's the code I'm using to get the guide to appear, this is within the onMouseMove function thats used when dragging the objects.

       

       

       


          var children:Array = parentCanvas.getChildren();
                    
                      for each(var child:* in children)
                      {
                                        
                          if(this != child)
                          {
                              if(this.x == child.x)
                              {
                                  parentCanvas.getChildByName("verticalSmart").visible = true;
                                  parentCanvas.getChildByName("verticalSmart").x = this.x;
                              }
                              else
                              {
                                  parentCanvas.getChildByName("verticalSmart").visible = false;
                              }
                          }
                      }

        • 1. Re: Smart Guides
          ATIF FAROOQ Level 3

          hi,

              try this code

                        var children:Array = parentCanvas.getChildren();
                        var alignedChildren:int
                        alignedChildren=0  
                        for each(var child:* in children)
                          {
                                      
                                     
                              if(this != child)
                              {
                                  if(this.x == child.x)
                                  {
          
                                      alignedChildren++
                                      parentCanvas.getChildByName("verticalSmart").visible = true;
                                      parentCanvas.getChildByName("verticalSmart").x = this.x;
                                  }
                                   
                              }
                          }
                         ///If no child Aligned With the dragged Object then hide the guide
                         if(!alignedChildren)
                         parentCanvas.getChildByName("verticalSmart").visible = false;
          • 2. Re: Smart Guides
            Seglespaan Level 1

            That Worked a treat, although I'm not entirely sure why,

             

            Thanks

            • 3. Re: Smart Guides
              ATIF FAROOQ Level 3

              Lets Simulate your Loop

              var children:Array = parentCanvas.getChildren();
                             
                              for each(var child:* in children)
                              {
                                                 
                                  if(this != child)
                                  {
                                      if(this.x == child.x)
                                      {
                                          parentCanvas.getChildByName("verticalSmart").visible = true;
                                          parentCanvas.getChildByName("verticalSmart").x = this.x;
                                      }
                                      else
                                      {
                                          parentCanvas.getChildByName("verticalSmart").visible = false;
                                      }
                                  }
                              }

               

               

              Assumptions:

              children.length=3

              children=[child1,child2,child3]

               

               

              First Run

              Assumptions:

              child=child1

              child1.x==child2.x

              this==child1

               

              if(this != child)//Condition evaluates to false as this==child

               

               

               

              Second Run

              Assumptions:

              child=child2

              child1.x==child2.x

              this==child1

               

              if(this != child)//Condition evaluates to true as this!=child
                 if(this.x==child.x)//Condition evalutes to true according to our assumption child1.x==child2.x
                   ///guides visibiliy set to true as both children are verticaly Aligned

               

               

              Third Run

              Assumptions:

              child=child3

              child1.x==child2.x

              this==child1

               

              if(this != child)//Condition evaluates to true as this!=child
                 if(this.x==child.x)//Condition evalutes to false according to our assumption child1.x==child2.x
                 else // else portion executes
                 parentCanvas.getChildByName("verticalSmart").visible = false;///guides are set to false again
              as you see visibility of the guides depend on the last iteration of the loop if it evaluates to true then guides will be visible otherwise not no matter wheter in previous iterations guide visibility was set to true or not so this creates problem . so you will need to find sum of all the objects that were aligned during the loop.If no object was aligned with the dragged object then set the guide visibility to false if there is more then zero alignments then set guides visibility to true.