5 Replies Latest reply on Sep 2, 2006 2:14 PM by wilson1978

    Actions on buttons

    wilson1978
      Hi all,

      ActionScript newbie here. I was just wondering how I can add ActionScript to a button that resides in the library and not on stage. Basically, I am trying to redo a flash site using ActionScript to place elements on stage. Here is the code I am using to load a menu mc:


      loadnav = function (temp:String) {
      if (temp == "modeling") {
      modeling_mc = this.attachMovie("modeling_mc", "modeling_mc","0",{_x:25, _y:108.8});
      }
      if (temp == "modeling") {
      modeling_mc = this.attachMovie("modeling_mc", "modeling_mc","0",{_x:25, _y:108.8});
      }
      else if (temp == "home") {
      home_mc = this.attachMovie("home_mc", "home_mc","0",{_x:25, _y:108.8});
      }
      }

      // calling the function on home
      loadnav("home");

      What I would like to do know is wire up the buttons to include code that passes changes the value of loadnav, thus, changing the menu movie clip.

      Hope that makes sense.

      Thanks
        • 1. Re: Actions on buttons
          Level 7
          wilson1978,

          > ActionScript newbie here. I was just wondering how I can
          > add ActionScript to a button that resides in the library and
          > not on stage.

          If you're going to use ActionScript to place assets on the Stage at
          runtime, then it only makes sense to add their ActionScript at runtime, too.
          That is, assign the ActionScript after the asset is already on the Stage.

          > Here is the code I am using to load a menu mc:

          Okay.

          > loadnav = function (temp:String) {

          Right off the bat, I suggest you make this a true named function.

          That is, this:

          function loadnav(temp:String) {...}

          ... instead of what you have. The reason is that in your version, loadnav
          can only be called *after* it has been declared. The function is a property
          of your loadnav object, rather than loadnav() being a Function object
          proper. If you do it the way I suggested -- using the function statement --
          you may call the function in statements that precede your declaration.

          > if (temp == "modeling") {
          > modeling_mc = this.attachMovie("modeling_mc",
          > "modeling_mc","0",{_x:25, _y:108.8});

          Okay, here, you've attached the modeling_mc asset and given it the
          instance name modeling_mc. So now that it has been instantiated as such,
          just make use of the events described in the MovieClip class (since, after
          all, this is a MovieClip isntance).

          modeling_mc.onRelease = function() {
          // instructions here
          }

          Make sense?

          Of course, you *can* put ActionScript into the timeline of any movie
          clip. That code is a part of that asset and gets carried with it when it's
          attached.

          // e.g. inside frame 1 of a movie clip in the Library
          this.onRelease = function() {
          trace("done been clicked");
          }

          But why bury your code "behind the scenes"?


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Actions on buttons
            wilson1978 Level 1
            Thanks for helping me out. I am just going to digest your recommendations. I will post back with how I got on. Hey, thanks for helping me out, as someone who has used flash without sensible AS, this is turning out to be a little tricky, so determined to get the concepts though.

            Thanks Again.
            • 3. Re: Actions on buttons
              wilson1978 Level 1
              One thing I didn't explain is that the movie clip, say home_mc, has buttons attached to that timeline, I would like to target the buttons that I have placed on the home_mc timeline and target these buttons from the main timeline.

              I would like to then have the buttons change the value of loadnav which in turn, will load a movie, so if the user clicks on the button modeling, the loadnav updates with the value modeling and then the action it will take is attach the asset modeling_mc.

              Hope that makes sense. I feel I have probably done this all wrong.
              • 4. Re: Actions on buttons
                Level 7
                wilson1978,

                > One thing I didn't explain is that the movie clip, say home_mc,
                > has buttons attached to that timeline, I would like to target the
                > buttons that I have placed on the home_mc timeline and target
                > these buttons from the main timeline.

                No problem. Just make sure each button has an instance name (select
                each, in turn, and add these in the Property inspector), then make sure the
                movie clip that contains them has an instance name. From the main timeline,
                the route to each button would be ...

                clipInstanceName.buttonInstanceName1
                clipInstanceName.buttonInstanceName2
                clipInstanceName.buttonInstanceName3

                Make sense? Just like nested folders on a hard drive.

                > I would like to then have the buttons change the value of loadnav
                > which in turn, will load a movie, so if the user clicks on the button
                > modeling, the loadnav updates with the value modeling and then
                > the action it will take is attach the asset modeling_mc.

                Sure. You've given loadnav() the ability to accept a parameter,
                arbitrarily named temp. So if you want one button to load another, just
                have it call the loadnav() function and pass in a different string -- for
                example, the modeling button could pass in home.

                clip.modeling_mc.onRelease = function() {
                loadnav("home");
                }

                > Hope that makes sense. I feel I have probably done this all
                > wrong.

                I'm not sure, because I can't really picture what you're aiming for. It
                is a bit tricky to have buttons create other buttons, that in turn determine
                which buttons are created. As long as you "get it," though, you should be
                all right.

                Note: in your loadnav() code, you're providing zero as the third
                parameter each time; that is, you're sending each button to the zero depth
                of the timeline it's intended for. By doing that, you're wiping out the one
                it's replacing. Not sure if you mean to do that or not. If you want
                several buttons on the Stage, you'd need to send each to its own depth.
                Note, too, that the MovieClip.attachMovie() method expects a number as its
                third parameter, not a string (your zero is in quotes). Plus, you're
                putting each button in the same horizontal and vertical location. Again,
                maybe you mean to do that ... if so, I wonder if each of these assets looks
                the same -- that is, if modeling_mc and home_mc look the same -- if they do,
                this is a very complicated way to change the functionality of a button. All
                these buttons seem to do is load other buttons that load other buttons. But
                they don't *do* anything. At least, they don't seem to, at this point. ;)


                David Stiller
                Adobe Community Expert
                Dev blog, http://www.quip.net/blog/
                "Luck is the residue of good design."


                • 5. Re: Actions on buttons
                  wilson1978 Level 1
                  Wow, thanks for that. You really know your stuff.