我是一個java/php開發人員,用actionscript幫助別人。我不明白爲什麼「this」在下面的代碼中是未定義的。這只是代碼的一小部分,但希望它能提供一個我想要引用「this」的概念。我試圖找出哪個電影的補間正在移動,以便我可以加載下一部電影。補間用於將電影移入或移出屏幕。「this」is undefined
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
UPDATE!下面是從反應/答案將提示後的工作代碼:
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
此外補間班的參考頁幫助。 http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/fl/transitions/Tween.html – 2010-07-23 20:10:59