A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [F8] event listener in objects

  1. #1
    Senior Member ihoss.com's Avatar
    Join Date
    Oct 2004
    Location
    Norway
    Posts
    581

    [F8] event listener in objects

    I have a major problem with event listeners and objects. This i a problem I've run into before, and I know there is a solution to it, but I have no idea what it is.

    I have a class called Game, with lots of functions. One of those functions is a function called mouseDown, and in the constructor of the class I set an MC's onMouseDown listener to call the function mouseDown. The problem is that when the function is called, this refers to the MC, not the instance of the class.

    Here is a simplified example:
    Code:
    var obj = {
    	something : 4,
    	mouseDown:function(){
    		trace(this);
    		trace(something);
    	}
    }
    trace(obj);
    _root.onMouseDown = obj.mouseDown;
    When you run this, and click the screen, you will get the following trace:
    Code:
    [object Object]
    _level0
    undefined
    _level0
    undefined
    _level0
    undefined
    _level0
    undefined
    The problem is that you cannot access any other member of the obj Object, for example the something variable. Anyone know how to fix this?

  2. #2
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Maybe is that instead of passing a reference to a function you should go with something like
    Code:
    _root.onMouseDown = function(){
    obj.mouseDown;
    }

  3. #3
    Senior Member ihoss.com's Avatar
    Join Date
    Oct 2004
    Location
    Norway
    Posts
    581
    Yeah, that would be easy, but the problem is that I'm going to have many instances of the object, so thats not really a solution.

  4. #4
    Senior Member ihoss.com's Avatar
    Join Date
    Oct 2004
    Location
    Norway
    Posts
    581
    Hokay, so I found a way it works. This way works because the anonymous function is in the scope of the constructor, so it knows what "that" is.
    Code:
    function obje(mc:MovieClip) {
    	var that = this;
    	mc.onMouseDown = function(){that.mouseDown()};
    	this.something = 4;
    }
    obje.prototype.mouseDown = function() {
    	trace(this == _root.obj);
    	trace(this.something);
    };
    var obj = new obje(_root);
    trace(obj.something);

  5. #5
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Them you can put all instances in a array and call the mouseDown of all the items
    But i bet this is not the most oopsist solution
    (edit) oops, only now i saw the previous post, nice to see the problem solved, in as3 things like that are not so cool anymore, flash used to be so simple and fun...
    Last edited by Cimmerian; 01-27-2008 at 04:47 PM.

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