5 Replies Latest reply on Mar 23, 2007 3:47 PM by rusew

    onData problem

    rusew
      Dear list, I have a class which opens an XMLSocket. I would like my onData function to access other functions in my class, but it is not working. I attached an approximation of my code.

      So, when I instantiate an object, I get this output when my server sends data:

      Connection Status: true
      connected java server
      Getting Data: imfromjava

      But then there's no "PrintData : imfromjava." It seems anything I call from onData will not work. It won't show an error either. Any ideas?

      Thanks!
      Rob

        • 1. Re: onData problem
          eric76
          It might be bacause of the confusing dynamic scoping rules that AS has and which still confuse me.
          Try replacing
          this.socket.onData = function(data:String) { ...

          with
          this.socket.onData = Delegate.create(this, myOnDataMethod);

          where myOnDataMethod() is defined separately in ur class. Look up Delegate on Google or something to see how to import it and use it.
          • 2. onData problem
            SymTsb Level 2
            Here's an easier method I learned from one of the other guys here. In my opinion, this is more efficient than Delegate and requires less overhead from having to import the includes....

            It is important to remember that this will reference the object directly affected by the current scope. Since you are inside onData, this is a reference to socket and not the actual class Megastream. As a result, your call to this inside onData is going to call socket.printData and not Megastream.printData.

            By declaring a class object... var msObj:Megastream = this; (It's important you type cast the variable), you now have access to the classes member functions from inside the buttons themselves.

            This is faster than Delegate in my honest opinion and requires less leg work.

            EDIT: This method will also give you access to your classes variables from inside the event handlers....something that delegate cannot do as I recall...hence the benefit.

            • 3. Re: onData problem
              eric76 Level 1
              Just a comment. Saying "It is important to remember that this will reference the object directly affected by the current scope." is not enough. You have to say "the current dynamical scope" - ie. scope semantics are dynamical. Most languages use statical scoping, and I think AS3.0 also does that.
              • 4. Re: onData problem
                SymTsb Level 2
                Yes my apologies. I was simply trying to make as easy to understand as possible. Often times, explaining stuff as a programming geek only worsen's peoples confusion. I always try my best to keep it as simple as possible to explain the current situation. No need to get into the full aspects of Flash scope as compared to c++ or c# or php. He just needs to know it's a scope issue and how to fix it.
                • 5. Re: onData problem
                  rusew Level 1
                  Thanks! This did the trick.

                  I understand "It is important to remember that this will reference the object directly affected by the current scope."

                  But I do not understand "statical scope" and "dynamical scope." What are those?

                  Many thanks,
                  Rob