A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: OOP; listeners and scope

  1. #1
    Senior Member SJT's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    2,563

    OOP; listeners and scope

    Ok, I'm going to pose a question...partly because I don't know the answer myself and I'd like it but also because I think scope is complicated question in OOP that could do with some explanation and discussion.

    Here goes; let's say I create an class and then instantiate it, how does the scope of this work from instance to instance.
    If this is the class, and it is placed on stage with attachMovie:
    code:
    Class = function(){
    trace(this);
    //the instantiated object on stage
    }
    Class.prototype = new Movieclip();
    Object.registerClass("myClip",Class);

    Class.prototype.myFunction = function(){
    trace(this);
    //this here is also the instantiated object on stage
    };



    However, this doesn't seem to be true for events.

    For example:
    code:
    Class = function(){
    trace("instantiated: "+this);
    //'this' is the instantiated object on stage
    Class.prototype.me = this;
    }
    Class.prototype = new Movieclip();
    Object.registerClass("myClip",Class);
    ASBroadcaster.initialize(Class.prototype);
    Class.prototype.addListener(Class.prototype);
    Key.addListener(Class.prototype);
    /*


    */
    Class.prototype.onMyEvent = function(){
    trace("Custom Event: "+this);
    //'this' here is NOT the instantiated object on stage
    trace("Custom Event: "+this.me);
    //we must pass a variable from the constructor to see what 'this' is on stage
    };
    Class.prototype.onKeyDown = function(){
    trace("Key Event: "+this);
    //'this' here is NOT the instantiated object on stage
    trace("Key Event: "+this.me);
    //we must pass a variable from the constructor to see what 'this' is on stage
    };
    Class.prototype.onEnterFrame = function(){
    if ( x < 2){
    this.broadcastMessage("onMyEvent")
    trace("frame: "+this);
    x++;
    }
    //'this' here is also the instantiated object on stage
    };
    Class.prototype.myFunction = function(){
    trace("Custom Function: "+this);
    //'this' here is also the instantiated object on stage
    };



    So, why is it that events except onEnterFrame don't have the same scope of this as other functions of an object?
    Sam



  2. #2
    Member
    Join Date
    Jul 2003
    Location
    Toronto, CANADA
    Posts
    64

    Re: OOP; listeners and scope

    Originally posted by SJT
    So, why is it that events except onEnterFrame don't have the same scope of this as other functions of an object?
    Part of the problem is you are defining the class as a listener, not an object, only objects can be defined as listeners (to the best of my knowledge). Second you need to put the ASBroadcaster and the two addListener lines in the class contructor, that way each time a new 'class' object is instantiated on the stage those three items get setup relative to the the new object.

    Class = function(){
    trace("instantiated: "+this);
    //'this' is the instantiated object on stage
    Class.prototype.me = this;

    ASBroadcaster.initialize(this);
    this.addListener(this);
    Key.addListener(this);
    }

    I you do the above then the traces you have work properly and each reference to 'this' traces the instance name.

    Hope this solves your problem.
    Flash Web Application Development
    www.innovasium.com

  3. #3
    Senior Member SJT's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    2,563
    Thanks, that does help explain things a lot.

    The problem that then occurs though, is how to add a listener from another class you've instantiated.
    It seems like a real pain to have to have to add each new object to the listener list when it's instantiated manually...that's what I was hoping adding the class would do instead.

    After a bit more investigation, it does add the class to the listeners list...sort of. Unfortunately it only adds the last instance created, not every instance created...
    Sam



  4. #4
    Member
    Join Date
    Jul 2003
    Location
    Toronto, CANADA
    Posts
    64
    You could try a static property class 'A', an array that contains each instance of the class 'A' instantiated objects. Then when you want to add a listener from a class 'B' loop through the array in class 'A' to get each instance.
    Flash Web Application Development
    www.innovasium.com

  5. #5
    Developer
    Join Date
    Sep 2001
    Location
    The Bluegrass State Will Flash For Food ™
    Posts
    3,789
    Didn't look at anything else but fyi, it's AsBroadcaster... In case you're using FP7 / Flash MX 2004...

  6. #6
    Senior Member SJT's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    2,563
    Thanks gSOLO_01,

    I haven't upgraded yet, is AsBroadcaster now a documented object?
    Sam



  7. #7
    Member
    Join Date
    Jul 2003
    Location
    Toronto, CANADA
    Posts
    64
    Originally posted by gSOLO_01
    AsBroadcaster
    Gotta love case sensitivity.
    Flash Web Application Development
    www.innovasium.com

  8. #8
    Developer
    Join Date
    Sep 2001
    Location
    The Bluegrass State Will Flash For Food ™
    Posts
    3,789
    Nope, still not documented. Although they use it in their components and such, there is a new EventDispatcher Object...

  9. #9
    Developer
    Join Date
    Sep 2001
    Location
    The Bluegrass State Will Flash For Food ™
    Posts
    3,789
    I've seen, when wading through some of Macromedia's components, that they do this also. i.e. this.constructor = this; they then reference the constructor rather than 'this'

  10. #10
    Senior Member SJT's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    2,563
    Hmm, that's interesting.

    So by referencing the constructor's 'this', does that make the class prototype the scope of this, rather than the object? Or the other way round...??
    Sam



  11. #11
    Member
    Join Date
    Jul 2003
    Location
    Toronto, CANADA
    Posts
    64
    Within the constructor (or the other methods) 'this' is a reference to the instantiated object. The scope of 'this' is the individual object.
    Flash Web Application Development
    www.innovasium.com

  12. #12
    Senior Member SJT's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    2,563
    Yeah, I've just realised I've already been doing this, but with slightly differently...oops

    I've been putting this in the constructor.

    code:

    Class(){
    Class.prototype._stagename = this;
    }



    Which is sort of the same thing really, just the other method is neater.
    Sam



  13. #13
    Member
    Join Date
    Jul 2003
    Location
    Toronto, CANADA
    Posts
    64
    Always more than one way to skin a cat.
    Flash Web Application Development
    www.innovasium.com

  14. #14
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    code:

    Class(){
    Class.prototype._stagename = this;
    }


    This function gets called each time an instance is created. So, Class.prototype._stagename will be changed and over-written each time.

    If we created 3 instances, mc1, mc2, mc3, at last, Class.prototype._stagename will be mc3. Then if we do trace(mc1._stagename), the output will be mc3 not mc1;

    What is "this" depends on "who" is calling the method. onEnterFrame is called by Clip itself. While the onMyEvent is called by the listener. Since we only make prototype object as listener, the listener is the prototype object not instance objects. So, "this" in onMyEvent is assumed to be prototype object.

  15. #15
    Senior Member SJT's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    2,563
    Oops, Class.prototype was a typo...I've typed it too much recently I think
    I did actually just use this._stagename...

    Nothing to see here, move on now...
    Sam



Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center