我是一名 java/php 开发人员,帮助某人使用 actionscript。我不明白为什么在下面的代码中未定义“this”。这只是代码的一个片段,但希望它能让我知道我在哪里尝试引用“this”。我试图找出补间正在移动的电影,以便我可以加载下一部电影。补间用于将电影移入和移出屏幕。
var tween_move_1:Tween = new Tween(movie_0, "_x", Strong.easeOut, 1600, 150, 0.5, true);
tween_move_1.onMotionFinished = function() {
stop();
setTimeout(function () {
trace(this);//when trace runs it shows undefined
var tween_move_2:Tween = new Tween(movie_0, "_x", Strong.easeOut, 150, 1600, 0.5, true);
tween_move_2.onMotionFinished = function() {
var tween_move_1:Tween = new Tween(movie_1, "_x", Strong.easeOut, 1600, 150, 0.5, true);
};
}
,2000);//end of setTimeout
};//end of tween.onMotionFinished
更新!这是应用响应/答案中的提示后的工作代码:
var tween_move_in:Tween = new Tween(movie_0, "_x", Strong.easeOut, 1600, 150, 0.5, true);
tween_move_in.onMotionFinished = function() {
stop();
var tweeny = this;//create reference to this so it can be used in setTimeout()
setTimeout(function () {
var movie = tweeny.obj;//use ref to get the movie affected by the tween
var movieName:String = movie._name;
var splitArray = movieName.split("_");
var index = parseInt(splitArray[1]);
var tween_move_out:Tween = new Tween(_root["movie_"+index], "_x", Strong.easeOut, 150, 1600, 0.5, true);
tween_move_out.onMotionFinished = function() {
var tween_move_in2:Tween = new Tween(_root["movie_"+(index+1)], "_x", Strong.easeOut, 1600, 150, 0.5, true);
};
}
,2000);//end of setTimeout
};//end of tween.onMotionFinished