22 Replies Latest reply on Apr 24, 2008 12:41 PM by onlymyidea

    How to add text to a drag and drop object

    mking10 Level 1
      Hi everyone, I am quite a beginner with Flash so do excuse me. First, I am not sure if what I want to achieve may actually be impossible with Flash. I presently use MX 2004, but will shortly be upgrading to CS3. But if you know the answer for MX 2004 that would be really useful for now.

      I am writing a digital fiction story, and I want to create drag and drop objects. This I have managed to do. But I would like these drag and drop objects to also reveal text when they are actually pressed on. I have had no success so far in being able to achieve the two effects together.

      Does anyone know how I can achieve this? If, by chance, you know the action script that would be great. If you can explain to me in simple terms how I can achieve this I really would be grateful.

      Thanking you,
      Mary
        • 1. Re: How to add text to a drag and drop object
          rlc5611 Level 1
          Place a dynamic text field in your object. For example, if I have a clip named "myclip" that I want to drag and I put a dynamic textfield inside that clip named "mytext" ----

          myclip.onPress = function() {
          this.mytext.text = "stop pressing so hard";
          startDrag(this);
          }
          myclip.onRelease = function() {
          this.mytext.text = "";
          stopDrag();
          }

          And that will work from Flash 6 onward.
          • 2. Re: How to add text to a drag and drop object
            kglad Adobe Community Professional & MVP
            create a textfield and assign its text property in your onPress method.
            • 3. Re: How to add text to a drag and drop object
              mking10 Level 1
              Thanks for getting back to me on that. Sorry, but can you explain to me how I place a dynamic text field inside an object. By object, you mean a movie clip, right? Not a button.
              Excuse me, but I really am a beginner with all of this. I have been trying to follow some tutorials on this stuff but they tend to presume that you already have a fair bit of knowledge. I pretty well have zilch.

              I have just sort of fathomed out how to create a dynamic text field, so that is a start.

              Thanks for you time and patience.
              • 4. Re: How to add text to a drag and drop object
                rlc5611 Level 1
                Yes a movieclip button rather than a button button. I haven't used a button button since Flash 5. I'm not even sure what they were for any more. If you double click on the movieclip, you can get inside its timeline and add anything you want.
                • 5. Re: How to add text to a drag and drop object
                  mking10 Level 1
                  Thanks for getting back to me on that. Hmmm! Well, I have been trying and it does not seem to be working. I am not sure what I am doing wrong. But this is what I am doing. I am importing a Bitmap image from my library to my stage and then converting it to a movie clip. I name it, as you suggested, my clip, and I also assign it the instance name my clip. Then, I double click on it and am taken to Scene 1 (away from the main stage area). There, in the Timeline, I create three layers. One is called my clip, one is called my text, and a third is called Actions (for the Action script).
                  On the my text layer, I use the text tool to draw a text box (my properties box is set to dynamic text, multiline, and I have assigned the instance name my text.
                  After clicking on the first frame of the Actions layer in the Time line, I hit F9 to open up the Actions area and put in the code you gave me.
                  myclip.onPress = function() {
                  this.mytext.text = "stop pressing so hard";
                  startDrag(this);
                  }
                  myclip.onRelease = function() {
                  this.mytext.text = "keep pressing hard";
                  stopDrag();
                  }
                  But when I hit Control Enter to preview my movie I can only see my image. I cannot drag it, and I cannot press it. It does not move or show any text. Have you any idea what I am doing wrong here?

                  Sorry to trouble you. I have been trying to find some basic tutorials on this, but none seem to relate to my specific needs.

                  Thanking you,
                  Mary
                  • 6. Re: How to add text to a drag and drop object
                    rlc5611 Level 1
                    The script I posted would work on the main timeline - not in the timeline of the clip itself.

                    So you made a movieclip out of the image and gave it the instance name "myclip". That's good.

                    You edited that clip and added a dynamic text box with instance name "mytext". Be careful not to confuse the "instance" name of the text box with the "Var:" name. "myclip" should be the instance name. Also be careful to make sure you uncheck the "selectable" box. Edit your clip and click on the textbox to select it and look in the properties panel. There should be a button with Ab on it. If it is white background (highly likely), click it so it is not white. If the text is selectable, it will interfere with your button functions.

                    While there is nothing wrong with creating nested clips within your movieclips, unless you plan to target them for something, there is no benefit. The only thing that needs an instance name inside the myclip is the textbox (based on what you are trying to do).

                    You don't need any actionscript in the movieclip itself. If you can learn to place all your AS on the root timeline, you will be well ahead.

                    If you placed that AS inside the clip, Flash wouldn't know what to do with it.

                    Technically you can place the AS inside the clip on the layer you created but, if you did that, you would need to modify the script to the following:

                    this.onPress = function() {
                    this.mytext.text = "stop pressing so hard";
                    startDrag(this);
                    }
                    this.onRelease = function() {
                    this.mytext.text = "";
                    stopDrag();
                    }



                    • 7. Re: How to add text to a drag and drop object
                      mking10 Level 1
                      Hi, I really appreciate the time you are spending with me on this one. Okay, well I have just worked on this again in the Timeline and I have checked all those pointers you mentioned, and this time in preview I could drag the object but I still could not get the text to show. I do not give Var. names. In fact, I do not even know what they are. I just give my fla. file a name, my layers name, my movie clips names and my instances names. I made sure that the AB button was not selected, so I am not sure what I am doing wrong. Hmmm!
                      I am using three layers in the timeline, but what I am creating is not time-based. I have an AS layer, a layer for myclip, and a layer for mytext.

                      Simply, I draw a dynamic text box on the stage under the myclip instance, and then put the AS you gave me in the AS layer. There is no other editing that I have to do to the movieclip.

                      This is the code I put in the AS layer

                      myclip.onPress = function() {
                      this.mytext.text = "stop pressing so hard";
                      startDrag(this);
                      }
                      myclip.onRelease = function() {
                      this.mytext.text = "keep pressing hard";
                      stopDrag();
                      }


                      I cannot fathom what I am doing wrong, but it is probably something really basic. Maybe something that you would really take for granted. I only started working with Flash recently. If you have any other ideas, please let me know.

                      Thanking you,
                      Mary
                      • 8. Re: How to add text to a drag and drop object
                        rlc5611 Level 1
                        The textbox must be inside the movieclip "myclip".

                        Above the timeline, you should see "Scene 1".

                        Can you click on the textbox so that it is outlined with a blue line and four drag corners?

                        If you can highlight that textbox and it still says only "Scene 1" above the timeline, then the textbox is not inside the clip.

                        What library name did the movieclip name when you made it a clip? I just made a clip in a new movie and the library name is "Symbol 1". If I double click on that clip, what shows above the timeline changes to "Scene 1 Symbol 1" which means I'm now inside the timeline of the clip. That is where the textbox need to go.

                        I think you are only a gnat's eyelash away from getting this.
                        • 9. Re: How to add text to a drag and drop object
                          mking10 Level 1
                          Hi there, I have just run through this all again. Yes, I have Symbol 1 in my library. I double clicked on it but the image sitting on the stage is a Bitmap, and there is no box for me to change it into an instance. At the top of my stage it says Scene 1 Symbol 1. I have made three layers, one for the image, one for the dynamic text, and one for Action Script.
                          The text box turns blue when I click on it, but it still says Scene 1 above it, so this means it is not inside the movie clip. So, sorry, how do I get it to go inside the movie clip? Also, I do not understand why this time I only see in the properties box that the image is Bitmap, and that I cannot give it an instance name. However, I did double click on the image and I made it Symbol no 2 and was able to give it an instance name myclip. I see that the properties box says I have an instance of Symbol, but if I look at the top of my Timeline it says Scene 1, Symbol 1. But if I double click on Symbol 2 in the library, I get Scene 1, Symbol 2, but once again the image on my stage is a Bitmap and I cannot give it an instance name.

                          So, I am chasing my tail here, but at least it is perhaps clearer to you how I am messing this up. So do you have any advice for this little gnat?

                          Thanking you,
                          Mary

                          • 10. Re: How to add text to a drag and drop object
                            rlc5611 Level 1
                            Place the attached script in the root timeline and test the movie and copy and paste the result.

                            Oh and I know this sounds stupid but make sure the color property of the text box is not the same color as the bitmap (i.e. what is behind it). I can say that because I just did that and was scratching my head over why no text was showing up. Dooh!

                            (a more grammatically correct version)
                            • 11. Re: How to add text to a drag and drop object
                              rlc5611 Level 1
                              And I'll just add that among all the things that show up, you want to see:

                              -------------------
                              myclip and it is a movieclip
                              and it contains:
                              onRelease and it is a function
                              onPress and it is a function
                              mytext and it is a object

                              What you do NOT want to see is:

                              -------------------
                              mytext and it is a object
                              -------------------
                              myclip and it is a movieclip
                              and it contains:
                              onRelease and it is a function
                              onPress and it is a function

                              or anything that is not the same as the first sample above.
                              • 12. Re: How to add text to a drag and drop object
                                mking10 Level 1
                                Hi there, thanks for getting back to me on this. I am still scratching my head. I am not sure what or where the root Timeline is, but I clicked on the first frame of the AS layer in the Scene/Symbol stage (Not the Main Scene stage) and then I hit F9 to open the Actionscript area. I copied and pasted all that code you put below, and then when I previewed I got a little box called Output. It said the following

                                things in _ root are:
                                ------------------------------
                                $ version and it is a string
                                -------------------------------
                                myclip and it is a movieclip
                                and it contains:
                                obj and it is a string
                                mytext and it is an object


                                Hmm! Okay, well I understand that this is not what I am supposed to have, but not sure what to do next. B.t.w., the image lay underneath this Output box, but I could not drag it. It was just static. The colour of the text box does not change when I change the colour property, so not sure what you meant there. I still get the black frame when drawing the box, and when I click on the box the outline is blue. Also, should I be hiding this textfield box behind the actual image or have it hiding the image on the stage? I have just been drawing the box so that it sits underneath the image, so that both text box and image are visible on the stage.
                                • 13. Re: How to add text to a drag and drop object
                                  rlc5611 Level 1
                                  Okay what this says is that the mytext textbox is, in fact, in the myclip clip. What is glaringly missing are your onPress and onRelease methods. For some reason you are not getting those applied to myclip. That would happen if you put the script in the wrong timeline.

                                  The would also happen if you put in that trace script before you established the onPress and onRelease. Make sure you put that trace at the tail end of your AS
                                  • 14. Re: How to add text to a drag and drop object
                                    rlc5611 Level 1
                                    And if the textbox is below the image, how do you expect to see it?
                                    • 15. Re: How to add text to a drag and drop object
                                      rlc5611 Level 1
                                      Okay this line:

                                      obj and it is a string

                                      proves that you are placing all your action script inside the myclip timeline rather than in the _root timeline.

                                      That particular line should not be there if the AS is correctly placed in the _root.

                                      You can always modify the script where it currently is as I indicated in one of the earlier posts where is said

                                      this.onPress....

                                      instead of

                                      myclip.onPress...

                                      But I suggest it is better to put the AS in the _root timeline.

                                      Go to wherever all your AS is. Use the mouse to drag and highlight all the AS in the AS window or else press <Ctrl><A> while the AS window is focused and cut that script.

                                      Next press "Scene 1" above the timeline. That will put you back in the _root timeline. Now paste all your AS in that timeline and test the movie.
                                      • 16. Re: How to add text to a drag and drop object
                                        rlc5611 Level 1
                                        Attached is a better version of the AS.

                                        Even better yet!
                                        • 17. Re: How to add text to a drag and drop object
                                          rlc5611 Level 1
                                          And you should see something like:

                                          things in _root are:
                                          -------------------
                                          $version and it is a string
                                          -------------------
                                          myclip and it is a movieclip
                                          and it contains:
                                          onRelease and it is a function
                                          onPress and it is a function
                                          mytext and it is an object



                                          you should NOT see something like:

                                          things in _root are:
                                          -------------------
                                          $version and it is a string
                                          -------------------
                                          myclip and it is a movieclip
                                          and it contains:
                                          obj and it is a string
                                          -------------------
                                          -------------------
                                          Hey - your script is in the wrong place!!!
                                          -------------------
                                          -------------------
                                          mytext and it is an object
                                          • 18. Re: How to add text to a drag and drop object
                                            mking10 Level 1
                                            Wow! Thank you really. It works. At last! I am really sooooo grateful to you, for all your patience and all that code.
                                            Can I dare just ask one more question? I would like to also to have a function whereby at first there is no text, then the viewer drags the object and they see the text, but when they release the object the text disappears. So the first two actions I can now do, but how can I make it so that when the viewer releases the object the text also disappears? Do you happen to know the code for that? I guess I just add that underneath the code:
                                            myclip.onRelease = function() {
                                            this.mytext.text = "keep on pressing";
                                            stopDrag();
                                            }

                                            Thanks though, really, for helping me with all that. I truly appreciate it.
                                            • 19. Re: How to add text to a drag and drop object
                                              rlc5611 Level 1
                                              That was in the very first post.

                                              When you create the textfield, don't put anything in it.

                                              for the onRelease handler, simply set text = "";

                                              Go back and read the earlier posts.
                                              • 20. Re: How to add text to a drag and drop object
                                                mking10 Level 1
                                                Ah, yes, you are right. It is all there. Thank you really. You are a genius. I have learned a lot here. Just have to let it all sink in.

                                                Thank you.
                                                • 21. Re: How to add text to a drag and drop object
                                                  rlc5611 Level 1
                                                  Targeting in Flash, once you get used to it, is really quite simple and foolproof. Always use trace() statements to help debug. The most useful trace statement I've ever seen is:

                                                  trace(this);

                                                  and the second most useful trace statement I've ever seen is:

                                                  trace(this._parent);
                                                  • 22. Re: How to add text to a drag and drop object
                                                    onlymyidea Level 1
                                                    mc.onPress = function()
                                                    {
                                                    mytext.text="pressing the mc";
                                                    }

                                                    mc.onRelease = mc.onReleaseOutside = function()
                                                    {
                                                    mytext.text="not pressing";
                                                    }