简体   繁体   中英

AS3 programming animation loop problems

I'm tinkering with making an object move from left a certain distance to the right, then start over from the left over and over.

I'm curious to how it works. Currently I have this (simplified) code where 'rect' is supposed to be looped to the right:

  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)

To me it seems that should cover it. If rect's position is more than 200 move it to 0, otherwise move it right by 10px on each frame. But all it does is move rect by a few pixels to the right then stops. Why doesn't this work, is the logic incorrect?

The following line of code resets the x position of the rect object:

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

This means that on every frame, the rect is moved to the above position, then moved 3.5 + a fraction between 0 and 1 pixels to the right. It will then be moved an additional 3.5 + a fraction between 0 and 1 pixels to the right.

So, on every frame the rect object will be moved to the x position of

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

The rect will therefore never appear to move much (a maximum of 2 pixels over the course of the load).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM