9 Replies Latest reply on Jul 29, 2009 5:49 PM by Lisa Chan

    Handler access

    Lisa Chan Level 1

      I was wondering how to access a method residing in a frame script from an object instance of a parent script. For example

       

      FRAME SCRIPT

       

      on beginSprite

      a = new (script "test")

      end

       

      on sayHello

        put "hello"

      end

      -----------

       

      PARENT SCRIPT "test"

      on new me

        ?sayHello()

      end

        • 1. Re: Handler access
          Chunick Level 3

          That's completely backwards.

           

          A parent script is a great way to encapsulate your code. Here's a great site by Irv Kalb which should clarify things for you: http://www.furrypants.com/loope/

          1 person found this helpful
          • 2. Re: Handler access
            Sean_Wilson Adobe Community Professional

            While I agree with Josh, you can use sendSprite(0, #handlerName) to target the frameScript channel. So in your case it would be:

            -- PARENT SCRIPT "test"
            on new me
              -- whatever else ...
              sendSprite(0, #sayHello)
            end
            
            • 3. Re: Handler access
              Lisa Chan Level 1

              Thank you both so much. The reason I want to use this method is because several classes will need to access a "setup" handler. But since it's only about 6 lines of code I will consider encapsulation. However, it's good to learn something new.

              • 4. Re: Handler access
                Chunick Level 3

                Lisa Chan wrote:

                 

                Thank you both so much. The reason I want to use this method is because several classes will need to access a "setup" handler. But since it's only about 6 lines of code I will consider encapsulation. However, it's good to learn something new.

                So, then why not a movie script to which you'd have global access to?... there are other options involving ancestors, but it all depends on how you're structuring things... unfortunately, the LOOPE articles don't get into ancestors... but for 6 lines of code a movie script would work well enough, I think.

                • 5. Re: Handler access
                  Lisa Chan Level 1

                  lol. I thought of that (movie script) but I *assumed* I would get the same result. I'll try that (I'm being lazy). Since you mentioned encapsulation: I was wondering if there is a way to pass system messages (in this case #keyUp) to child objects that are not attached to sprites or cast members. and If I use the same sendSprite(0,#keyUp) method would I implicity receive the pertinent parameters? (I'm used to Flash AS, so I hope u know where I'm coming from)

                  • 6. Re: Handler access
                    Lisa Chan Level 1

                    the movie script does the job nicely thanks. I'm still trying to figure out the best way to access the #keyUp script without attaching the child object to the sprite as a behavior.

                    • 7. Re: Handler access
                      Lisa Chan Level 1

                      Now that I think about it, an ancestor script may be the best choice. All of the child objects that will use this all have a common root (rigid bodies). I hope I'm not being annoying. I just think someone else may find this helpfull if I verbalize my thoughts and findings. I'm such a noob, lol.

                       

                      btw for anyone else reading this the ancestor is just a base/super class implemented like this:

                       

                      PARENT SCRIPT

                       

                      property ancestor

                       

                      on new me

                        ancestor = new script("myParent")

                      end

                      • 8. Re: Handler access
                        Production Monkey Level 3

                        You can take global control over KeyUp events by routing them through a Parent script instance. This is done by assigning some code to the  "keyUpScript" property. Other objects then register with the Keyboard Manager object to recieve keyUp message calls. A basic implementation of such an object would be:

                         

                        -- Keyboard Manager Parent script

                         

                        property KeyUpEvents  -- list of events and objects registered to be called when a keyUp event happens.


                        on new me
                          KeyUpEvents = []
                          return me
                        end

                         

                         

                        on addEventListener me, CallbackObject, CallbackHandler  -- script instance or sprite reference, Symbol
                          -- may want to add argument validation here.
                         
                          CallBackInfo = [#CallbackObject:CallbackObject, #CallbackHandler:CallbackHandler]
                          KeyUpEvents.add(CallBackInfo)
                        end addEventListener


                        on keyUp me
                        --  put _key.key  -- for debuggin
                         
                          repeat with CallBackInfo in KeyUpEvents
                            call(CallBackInfo.CallbackHandler, CallBackInfo.CallbackObject, _key.key )
                          end repeat
                        end

                        -------------------------------------------------------------------

                         

                        A Parent script that wants to receive keyUp messages would be:

                         

                        --  gKeyMan sample Parent script

                         

                        global gKeyMan

                         

                        on new me
                          gKeyMan.addEventListener(me, #callBackMethodName)
                          return me
                        end

                         

                        on callBackMethodName me, KeyPressed
                          put "Key pressed was: " & KeyPressed
                        end

                        ------------------------------------------------------------------------------------------ ------

                         

                        A movie script to test the two parent scripts would be:

                         

                        -- prepare movie script

                         

                        global gKeyMan

                         

                        on prepareMovie
                          gKeyMan = script("Keyboard Manager").new()
                          the keyUpScript = "gKeyMan.keyUp()"
                         
                          KeySample = script("gKeyMan Sample ").new()
                        end


                        on exitframe
                          go the frame
                        end

                        ------------------------------------------------------------------------------------------ ------

                         

                         

                        One note:  I suggest not using "sendSprite()".  If you call a method on a sprite using sendSprite and that method does not exist you get no error. If I made a mistake like that I would want an error to pop up so that I can fix it. Sprites are objects that inherit the methods and properties of the behaviors that are attached to them. Simply call the sprite's methods directly. ie  sprite(x).doSomething().

                        1 person found this helpful
                        • 9. Re: Handler access
                          Lisa Chan Level 1

                          Thank you, Production Monkey. Very impressive approach. I never would have thought of that.

                          edit: I didn't read through the code completely before responding. But I've got it now. 

                          Thanks again, everyone.

                          I am very impressed by all of the responses.