2010-07-23 115 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);