AS3编程动画循环问题

时间:2014-07-02 09:26:54

标签: actionscript-3 loops animation

我正在修补一个物体从左边移动一定距离到右边,然后一遍又一遍地从左边开始。

我很好奇它是如何运作的。目前我有这个(简化的)代码,其中' rect'应该在右边循环:

  ok, this is the code non simplified: function preload(e:Event):void{
    var loadedBytes=loaderInfo.bytesLoaded;
    var totalBytes=loaderInfo.bytesTotal;
    var rect=MovieClip(root).loader_rect;
    var startpos=rect.x=stage.x-rect.width;

       if(loadedBytes==totalBytes){
           removeEventListener(Event.ENTER_FRAME, preloader);
           gotoAndStop(2); 
       }
       else{ 
           rect.x+=3.5+loadedBytes/totalBytes; 
         if(rect.x>=stage.x+stage.width) rect.x=startpos  

           else rect.x+=3.5+loadedBytes/totalBytes; 
       }
} (I get no errors, just  the animation isn't working)

对我而言似乎应该涵盖它。如果rect的位置大于200,则将其移至0,否则在每帧上向右移动10px。但它所做的只是向右移动几个像素然后停止。为什么这不起作用,逻辑不正确?

1 个答案:

答案 0 :(得分:0)

以下代码行重置 rect对象的x位置:

var startpos = rect.x = stage.x - rect.width; 

这意味着在每一帧上,rect移动到上面的位置,然后向右移动 3.5 + 0到1 像素之间的分数。然后它将一个额外的 3.5 +一个0到1 像素的分数向右移动。

因此,在每一帧上,rect对象将移动到

的x位置
stage.x - rect.width + 3.5 * 2 + [percent loaded] * 2

因此,rect似乎永远不会移动太多(在加载过程中最多2个像素)。