18 Replies Latest reply on Jan 14, 2010 6:53 AM by Markstar.Adbe

    "Anchor" for objects?

    Markstar.Adbe

      Hi,

      is there a way to have (the postion of) objects be dependant of another one? To illustrate my question, I have attached a simple .fla file with two boxes (MovieClips) that grow in height. Since the position of the bottom box is constant, the top box soon expands "into" the one below so that parts of it are hidden. What I want is that the gap between those two objects stays constant (in this case 50px).

       

      So, is there any way to make the position of one object dependant on (the size of) another? Or is there any other "workaround"/tip you can give me? Please keep in mind that I am very new to Flash/AS3!

       

      Thank you in advance!

        • 1. Re: "Anchor" for objects?
          kglad Adobe Community Professional & MVP

          yes, but you'll need to code for that.

           

          for example, in the same loop that repeatedly changes the size of your box, change its position.

          • 2. Re: "Anchor" for objects?
            Markstar.Adbe Level 1

            Sorry, I'm not sure what you mean.

             

            So far, I don't have a loop. As you can (maybe) see from my example, I created everything with Flash so far (no AS3 yet).

             

            I have no idea how change the position via code dependent on the size of another object (especially since these will be nested objects).

             

            Could you please elaborate or maybe even give me an example?

             

            Thanks you for your time!

            • 3. Re: "Anchor" for objects?
              kglad Adobe Community Professional & MVP

              if you're using the timeline to tween the size of one of your boxes, use the timeline to tween its position.

              • 4. Re: "Anchor" for objects?
                Markstar.Adbe Level 1

                But this only works in some scenarios and is not a solution for my problem because it would require me to manually adjust the position for each scenario.

                 

                Consider my (unfinished) side menu for my website: http://markstar.x10hosting.com/test/sidemenu03.html

                 

                My idea was that each main menu entry could be a movie clip that expands if necessary (sadly I do not have the skill to do this all via AS3 yet). Furthermore, there are supposed to be more boxes of various content underneath that should also slide up/down if neccessary.

                 

                So the question still remains (but I'm beginning to fear that it is not possible) - is there any way to link the position of one object to another and if so, how?

                 

                One idea I just had is that you have a global variable that is a sum of all the relevant heights to determine he Y-coordinates for that box, e.g.:

                     height_box_a + height_box_b + height_box_c + gap_constant + height_box_d + gap_constant2 = y_box_e

                 

                But I have no idea how one would implement this in AS3.

                • 5. Re: "Anchor" for objects?
                  kglad Adobe Community Professional & MVP

                  using code for some of your menu animation and the timeline for some of your menu animation will be an unholy mess.  either use code for all your menu animation or use the timeline for all your menu animation or use the accordian component.

                  • 6. Re: "Anchor" for objects?
                    Markstar.Adbe Level 1

                    Programming everything from scratch is out of the question right now (this would just take way to long to learn and implement right now).

                    So is the accordion solution (it only solves a minor part of the problem and creates several new ones).

                     

                    Creating a "mess" seems to be the only solution within Flash for me (besides switching back to JavaScript, which is exactly what I wanted to avoid).

                     

                    I find it very interesting though that this is not easily doable since from what I have seen, Flash is to automatically connect/combine shapes. Creating an anchor to move objects relative to each other does not sound like too complicated.

                    • 7. Re: "Anchor" for objects?
                      kglad Adobe Community Professional & MVP

                      if you want to pursue the "mess" then you are taking the path of maximum difficulty for yourself.

                       

                      you'll need to start a loop (like onEnterFrame) everytime a box changes size and you should stop that loop when your box stops changing size.  in that loop you'll need to reference all instances that need to respond to the box changing size and you'll need a reference to the box that's changing size.  then you'll need to code for the changes you want.

                       

                      just start with the easiest part of that.  create an instance name for the box that's changing shape.  to do that you'll need to convert the box to a movieclip.  see if you can do that while maintaining your timeline tween.

                      • 8. Re: "Anchor" for objects?
                        Markstar.Adbe Level 1

                        kglad wrote:

                         

                        if you want to pursue the "mess" then you are taking the path of maximum difficulty for yourself.

                         

                        you'll need to start a loop (like onEnterFrame) everytime a box changes size and you should stop that loop when your box stops changing size.  in that loop you'll need to reference all instances that need to respond to the box changing size and you'll need a reference to the box that's changing size.  then you'll need to code for the changes you want.

                         

                        just start with the easiest part of that.  create an instance name for the box that's changing shape.  to do that you'll need to convert the box to a movieclip.  see if you can do that while maintaining your timeline tween.

                        1) So you think my idea of adding up the heights is not viable? Wouldn't I "just" have to be able to ask for each object and then position the objects accordingly? Or is that what you want your loop to do? BTW, so far the only thing I have coded is "stop();". While I am experienced in C++ (and HTML, some JavaScript and PHP), I have no idea how a loop over several frames would work. But I admit I have not really looked into AS3 yet.

                         

                        2) I have done what you said (basically the example I attached in the first post was already like that). However, I don't know what you mean by

                        see if you can do that while maintaining your timeline tween.

                         

                        Again, thank you so much for your time!

                        • 9. Re: "Anchor" for objects?
                          kglad Adobe Community Professional & MVP

                          forget the details of the loop.  just see if you can accomplish step one of this approach.

                           

                          can you reference the object whose height is changing?  ie:

                           

                          trace(yourmovieclip._height);  // can you assign yourmovieclip to the box that's changing height?

                          • 10. Re: "Anchor" for objects?
                            Markstar.Adbe Level 1

                            Since I didn't really know how to reference objects or what the line of code meant that you gave me (I could take a guess from my previous programming knowledge though), I decided to give AS3 some more attention.

                             

                            So yes, now I have successfully referred to the boxes and with the help of ENTER_FRAME I even learned how to get the heights of the boxes on each iteration.

                             

                            So, what do you suggest is next?

                             

                             

                            Edit: For my simple example the question is quite obvious I guess:

                             

                            stage.addEventListener(Event.ENTER_FRAME, heights);
                            
                            function heights(evt:Event):void
                            {
                                 trace(box0.height);
                                 box1.y = box0.height + 50;
                            }
                            

                             

                            This automatically adjusts the position of the second box. However, it is neither dynamic nor does it account for submenus (which can have a height but simply not be displayed (hmm, this could be avoided by adding a frame at the beginning which returns a 0... I don't know how yet but it might work. )).

                            • 11. Re: "Anchor" for objects?
                              kglad Adobe Community Professional & MVP

                              that's dynamic.  box1's position should respond in real time to box0's height.

                               

                              now you need to add all the objects that need to respond and you need to add all the objects to which they should respond.

                              • 12. Re: "Anchor" for objects?
                                Markstar.Adbe Level 1

                                kglad wrote:

                                 

                                that's dynamic.  box1's position should respond in real time to box0's height.

                                Well, yes. But it's not dynamic in the sense that the code will automatically find and compute all the available heights. I still have to manually "register" each box by writing the variables (I was hoping to solve this via an array or something like that).

                                now you need to add all the objects that need to respond and you need to add all the objects to which they should respond.

                                Indeed. I am running into a little problem there, however:


                                Let's say I have two instances of a MovieClip, box0 and box1, which I can control with AS from the stage (eg. box0.visibility, etc). However, I can't access any outside variables from within the instances (for example, inside of box0 I can't say: stage.box1.visibilty = true;). This makes sense I guess, but does complicate things.

                                • 13. Re: "Anchor" for objects?
                                  kglad Adobe Community Professional & MVP

                                  1.  you can use an array (or arrays), if that helps you.  you'll need to create the array(s).

                                   

                                  2.  you can reference any movieclip timeline (that has an ancestor in the displaylist) from any other timeline (that has an ancestor in the playlist).

                                   

                                  for example:

                                   

                                  from box0's timeline, if box0 and box1 are on the main timeline, you can use

                                   

                                  MovieClip(root).box1.visible = true;

                                  • 14. Re: "Anchor" for objects?
                                    Markstar.Adbe Level 1

                                    kglad wrote:

                                     

                                    1.  you can use an array (or arrays), if that helps you.  you'll need to create the array(s).

                                    Obviously.

                                     

                                    2.  you can reference any movieclip timeline (that has an ancestor in the displaylist) from any other timeline (that has an ancestor in the playlist).

                                     

                                    for example:

                                     

                                    from box0's timeline, if box0 and box1 are on the main timeline, you can use

                                     

                                    MovieClip(root).box1.visible = true;

                                    Ahh, cool. That's what I was looking for. I tried reading and only found root or _root, but those didn't work, so I solved that issue with the help of a lot of if-clauses.

                                     

                                    I have another problem now, but have opened up another thread for this (see here: http://forums.adobe.com/thread/555467 ), because it has really nothing to do with "anchors".

                                     

                                    Cheers and thanks again so much for your help!

                                    • 15. Re: "Anchor" for objects?
                                      kglad Adobe Community Professional & MVP

                                      you're welcome.

                                       

                                      p.s.  if this thread is answered, please mark it as answered.

                                      • 16. Re: "Anchor" for objects?
                                        Markstar.Adbe Level 1

                                        kglad wrote:

                                        p.s.  if this thread is answered, please mark it as answered.

                                        Ahh, yes. I shall do that. However, I am still wondering where I need a while loop or why this solution is really messy. It is all pretty straightforward (now that I have worked with the AS3 syntax some) IN THEORY (in reality it's not because AS3 seems to be buggy - see the other thread).

                                        • 17. Re: "Anchor" for objects?
                                          kglad Adobe Community Professional & MVP

                                          as i said way back there in this thread, it's messy because you're mixing timeline tweening with actionscript.  and, you might find it's more than messy:  it may be impossible.

                                          • 18. Re: "Anchor" for objects?
                                            Markstar.Adbe Level 1

                                            Yes, I guess your are right. For example, the next problem (here: http://forums.adobe.com/thread/555967 ) is that if I resize objects via ActionScript, the resulting Object is not always an integer, even though I tell them to be.

                                             

                                            It does not make any sense whatsoever!!!