|
-
'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;
}
-
'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....
-
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.
-
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
-
is there a difference in using "currentTaget" and "target"?
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|