4 Replies Latest reply on Jun 8, 2007 1:02 AM by zibber

    passing multiple arguments via function

    zibber Level 1

      i wondered if someone could help me.

      I am building a simple app that allows people to drag different type of assets onto an image.

      The thing is i want to be able to alter the height of a particualr movie clip in one of these assets. But ideally i want to keep the amount of functions i have to a minimum to keep it nice and tidy.

      The problem i have is how to pass data via a function on an event listener.

      As an example say i have 3 assets a user can select. What i am doing currently is creating a listerner for each asset. But whena user clicks one i want it to use the same function but jsut pass a reference to a library asset for each one. So my listeners would look like:

      caption1.addEventListener(MouseEvent.MOUSE_UP, addCaption);
      caption2.addEventListener(MouseEvent.MOUSE_UP, addCaption);
      caption3.addEventListener(MouseEvent.MOUSE_UP, addCaption);

      But ideally here pass a name of a liray asset to addCaption. So in addcaption i can create a new movieclip and then add a clip formthe library into it. Here is my function so far:

      function addCaption(event:MouseEvent):void {

      captionNum ++;

      // add in caption clip
      var captionClip:MovieClip;
      captionClip = new MovieClip()
      captionClip.name = "caption" + captionNum;
      captionBG = new MovieClip()
      captionBG = new bubble1() <<<<<<<THIS IS WHAT I WANT TO BE CALLED IN DYNAMIC

      captionClip.x = 20;
      captionClip.y = 200;

      captionClip.doubleClickEnabled = true;
      captionClip.captionBG.doubleClickEnabled = true;

      captionClip.addEventListener("doubleClick", doubleClick);
      captionClip.addEventListener("click", setSelect)


      the reaosni want to do it this way is so i can just reference the pulled in clip by its variable name captionBG as my previous way was restrictive as i had to go something like:

      SelectedObject.bubble1bg.height = SelectedObject.myText.height + 50;

      so ideally i would like ot be able to go:

      SelectedObject.captionBG.height = SelectedObject.myText.height + 50;

      i hope that makes sense, and i doubly hope someone can help me:) im sure this is simple but i cant work it out:|

      thanks in advance
        • 1. Re: passing multiple arguments via function
          kglad Adobe Community Professional & MVP
          i'm not sure what you're trying to do but the basic issue with as3 is you can't assign custom properties/values to display objects that can then be later retrieved (like in your listener function).

          however, you can assign custom properties/values to the object class in as3 and you can assign an object to displayobjects. so, create an object property of you displayobject and assign it whatever properties/values you need to retrieve in your listener function.
          • 2. Re: passing multiple arguments via function
            zibber Level 1
            thanks kglad but im still a bit stuck. The below may be a better explantion:

            var captionClip: MovieClip;
            var captionBG: MovieClip;

            function createCaption(){
            captionClip = new MovieClip;
            captionBG = new MovieClip;
            captionBG = new one();

            butone.addEventListener(MouseEvent.MOUSE_UP, createCaption);
            buttwo.addEventListener(MouseEvent.MOUSE_UP, createCaption);

            so ideally something like:

            buttwo.addEventListener(MouseEvent.MOUSE_UP, createCaption(two));

            so i can just tell the function which clip to add into captionBG. Or am i completly barking up the wrong tree and going about things shockingly badly? :)

            thx in advance

            So i have two button clips on my stage who listen for a mouseup(or whatever), once one is picked i would like to be able to tell captionBG to pull out either "one" or "two" movie clip, linked in the library
            • 3. Re: passing multiple arguments via function
              kglad Adobe Community Professional & MVP
              you're going about it wrong. you can't change the fact that with as3 you CANNOT pass anything to the listener function other than a reference to the event. but the event (MouseEvent in your situation) carries a reference back to your event dispatching objects (butone, buttwo etc). so, if you could create a reference to "one" in your dispatching object butone and a reference to "two" in buttwo, you could use that reference to extract what you need.

              now, you could just use the name property of the dispatching object. and that may be all you need:

              • 4. Re: passing multiple arguments via function
                zibber Level 1
                many thanks Kglad i was getitng there but your code helped finsih it off. almost:| my next struggle is how to tie that to the actual linked file in the library.

                eg something like:

                captionBG = new trace(me.currentTarget.name);

                but obviosuly the above ownt work. im stuk wihtout eval:( ;)