2010-07-23 112 views
1

我是一個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 
+0

此外補間班的參考頁幫助。 http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/fl/transitions/Tween.html – 2010-07-23 20:10:59

回答

0

如果你試圖描繪出tween_move_1,你可以參考它直接setTimeout()內。

+0

Thx以及Daniel下面的提示指出了我的正確方向。 – 2010-07-23 20:05:30

0

好吧,這裏是怎麼了......

當您使用new function() {this}作爲SetTimeout(function() {

這將創建一個空的(undefined)對象,這個對象與調用函數的對象不一樣

雖然我不能告訴你應該怎麼做,因爲我不知道你在做什麼,我希望這有幫助你弄明白了。

,你可以既但引用函數(VAR富:功能......),並傳遞一個變量foo的($ VAR:類型)

0

如果你想通過在特定this是一個可用在您定義tween_move_1的範圍內,然後創建另一個局部變量,用this填充它,然後使用該新變量。

var tween_move_1:Tween ... 
var foo:* = this; 
... 
    setTimeout(function() { 
     trace(foo);