Oct 30, 2008

ActionScript: Passing parameters to onRelease and other event functions

As it turns out, you can't use parameters directly when defining an event function, for example:
myMovie.onRelease = function(param1,param2,param3) {
   //my code
}
The example above will generate a compiler error. This won't work either:
var = 1;
var2 = "something else";
myMovie.onRelease = function() {
   //var and var1 are unaccessible at this point
}
The solution for this is simple, yet effective. You need to define your paramaters to a movieclip, so that you can reach them with the operator "this".
myMovie.param1 = "something";
mymovie.param2 = "something else";
myMovie.onRelease = function() {
   trace(this.param1);
   trace(this.param2);
}

No comments: