A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: 'this' in as3 doesnt seem to work anymore

  1. #1
    Member
    Join Date
    Feb 2004
    Posts
    44

    'this' in as3 doesnt seem to work anymore

    hi guys
    can anyone tell me why this code isnt working - actually it does work, but clicking on any of the 4 movieclips executes the function on all of the clips rather than just the one clicked

    if that makes any sense! used to work fine in as2!!! cheers

    var cube:Array = new Array();
    cube = ['1','2','3','4'];
    for (var i:uint=1; i<5; i++) {

    this["cube"+i].addEventListener( MouseEvent.CLICK, mouseclick);
    }

    function mouseclick( e:MouseEvent):void {
    this.scaleX = 2;
    this.scaleY = 2;
    }

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    'this' is a class not an array...you want to target

    PHP Code:
        cube[i].addEventListener(); 
    Although I'm not sure why you would listen to an integer....

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well yes, there is that. But assuming that he managed to add his listener function to the correct things, there's still a subtler issue.

    In AS2, "this" referred to whatever object was executing the function. You'd assign a function as a property of an object (onRelease = function()...) and "this" in that function would refer to the object on which the event was triggered.

    In AS3, "this" does not change depending on the object executing the function, so his code above would scale the whole movie, and not just the clicked instance.

    Instead of "this.scale", use "e.currentTarget.scale"
    Last edited by 5TonsOfFlax; 04-30-2008 at 03:25 PM.

  4. #4
    Member
    Join Date
    Feb 2004
    Posts
    44
    hey 5tonsoflex, i could kiss you, that works perfectly

    i couldnt understand why 'this' wasnt behaving the same way it had in as2 - thanks so much for your help

    steve

  5. #5
    Senior Member
    Join Date
    Jun 2007
    Posts
    204
    is there a difference in using "currentTaget" and "target"?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes. currentTarget will always be the instance on which you added the event listener. target may be another instance which triggered an event which bubbled up.

    If you add a MouseEvent.CLICK listener to the Stage, currentTarget will always be the stage. But target may be anything on the stage which was clicked.

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