Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Calling Private Class Method From Within An Event Handler

    I have the following code to call the private class method 'foo' when 'handleConnect' is called (an onConnect handler for an XMLSocket object):
    Code:
        private function foo(nameS:String):String {
            trace("HI, " + nameS);
            return "Hi, " + nameS;
        }
        private function handleConnect(success:Boolean) {
            if (success) {
                this.connected = true;
                trace("before");
                trace(this.foo("Bar"));
                trace("after");
            }
        }
    The method 'foo' is never being called and the output is:
    before
    undefined
    after
    Yet if I make 'handleConnect' public and call it from the first frame, it works just fine. Anyone know why it's not calling 'foo' when 'handleConnect' is called on an XMLSocket onConnect event?

    EDIT: I found in the Flash help an article about event handler scope and learned that the scope is not the event handler method's parent object but rather the object that threw the event. From this information and some provided sample code, I decided to try this code in my constructor (yes, the class is called Uzume):
    Code:
        public function Uzume(host:String, port:Number) {
            var owner:Uzume = this;
            this.connection = new XMLSocket();
            this.connection.onConnect = function (success:Boolean) {
                owner.handleConnect(success);
            }
            this.host = host;
            this.port = port;
        }
    And now I got it working >.<
    Last edited by thesmart1; 01-18-2009 at 02:24 AM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •