;

PDA

Click to See Complete Forum and Search --> : 'this' in as3 doesnt seem to work anymore


sdm038
04-30-2008, 02:23 PM
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;
}

neznein9
04-30-2008, 02:53 PM
'this' is a class not an array...you want to target


cube[i].addEventListener();


Although I'm not sure why you would listen to an integer....

5TonsOfFlax
04-30-2008, 03:22 PM
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"

sdm038
04-30-2008, 04:38 PM
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

moose-o
05-04-2008, 05:15 PM
is there a difference in using "currentTaget" and "target"?

5TonsOfFlax
05-04-2008, 05:18 PM
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.