6 Replies Latest reply on Jul 26, 2007 7:35 AM by Newsgroup_User

    Calling a private function when inside another class?

    Kenchu1
      Can't help it but im curious how classes seem to be able to call private functions inside other classes. I'm mainly thinking about the addEventListener() here. When adding a listening function to a class, that function can be private, and yet it seems to be called magically somehow. Or maybe its all internal? I dunno. Anyone? :D
        • 1. Re: Calling a private function when inside another class?
          kglad Adobe Community Professional & MVP
          how are you calling a private function in one class from another class?
          • 3. Re: Calling a private function when inside another class?
            kglad Adobe Community Professional & MVP
            exactly what? show me one class calling another class to which it has no access.
            • 4. Re: Calling a private function when inside another class?
              Level 7
              Hi Kenchu1,

              Are you referring to ActionScript 2.0 or 3.0? In 2.0 this was
              completely do-able. The terms "private" and "public" were mild
              suggestions :)

              In ActionScript 3.0, this is accomplished by having an intermediary.
              You don't call the function directly, you do it via a reference. Or at
              least that's my guess. I wrote my own event broadcasting system,
              completely separate from the way Flash does it. I just set my class
              methods to "private" and "protected" and both worked. They shouldn't
              have from what I understand, but there you have it. Maybe there's
              another explanation for this.

              Patrick

              Kenchu1 wrote:
              > Can't help it but im curious how classes seem to be able to call private
              > functions inside other classes. I'm mainly thinking about the
              > addEventListener() here. When adding a listening function to a class, that
              > function can be private, and yet it seem to be called magically somehow. Or
              > maybe its all internal? I dunno. Anyone? :D
              >

              --
              http://www.baynewmedia.com
              Faster, easier, better...ActionScript development taken to new heights.
              Download the BNMAPI today. You'll wonder how you ever did without it!
              Available for ActionScript 2.0/3.0.
              • 5. Calling a private function when inside another class?
                Kenchu1 Level 1
                Patrick B,
                exactly! Something like that is what im looking for. I have my own rather simple system right now with listeners and classes calling these, but since i dont know how to call a private function, i had to make all the listening classes functions public - something id rather avoid. Exactly how did your event broadcasting system work? Oh, and we're talking AS3 btw.

                How do i call a private function via a reference? (to the function? To what?)
                • 6. Re: Calling a private function when inside another   class?
                  Level 7
                  Hi Kenchu1,

                  You can grab a copy of the open source API here:
                  http://www.baynewmedia.com/download/BNMAPI10.zip (feel free to drop by
                  the main site for documentation as well :) ). The main class is the
                  Events class, "broadcast" method. This method broadcasts events in a
                  decoupled fashion, meaning that listeners can listen to messages that
                  aren't bound to a specific sender. The AS3 version works in much the
                  same way except that I still haven't fully fleshed out the cross-SWF
                  communication that this version can do (broadcast across all movies on
                  the same computer using LocalConnection).

                  Basically, the broadcaster works like this:

                  1. Add event listener and bind to function A (called from within the
                  class so the reference is available)
                  2. Event listener pushes this into the listener array. It was provided
                  by the class so the reference is valid and is now a part of the events
                  class as well.
                  3. Broadcast runs through all associated events when it comes time to
                  broadcast and calls the function by using the array reference:
                  this.listeners[message].call(classInstance,someParameter);

                  In other words, the class that adds the listener "allows" the event
                  broadcaster to use the reference because it passes it out. You can do
                  the same thing by calling: someOtherclass.functionRef=this.privateFunction
                  someOtherClass is the class that will store the private reference,
                  functionRef is some variable to hold the reference, and privateFunction
                  is the private function. Then, in someOtherClass, you can call:

                  this.fuctionRef(someParameter);

                  Hope this helps.

                  Patrick

                  Kenchu1 wrote:
                  > Patrick B,
                  > exactly! Something like that is what im looking for. I have my own rather
                  > simple system right now with listeners and classes calling these, but since i
                  > dont know how to call a private function, i had to make all the listening
                  > classes functions public - something id rather avoid. Exactly how did your
                  > event broadcasting system work? Oh, and we're talking AS3 btw.
                  >
                  > How do i call a function via a reference? (to the function? To what?)
                  >

                  --
                  http://www.baynewmedia.com
                  Faster, easier, better...ActionScript development taken to new heights.
                  Download the BNMAPI today. You'll wonder how you ever did without it!
                  Available for ActionScript 2.0/3.0.