2011-04-11 36 views
0

我正在尝试将位图加载到舞台上,然后在完全使用AS代码时补间它。下面的工作,但是当它添加一个新的位图图像的阶段,它离开最后一个,从而留下一个相同的位图的负载。基本ActionScript 3 sprite补间

任何想法?我尝试添加“removeChild(myLoader);”但那没有做什么。非常感谢。

import flash.display.MovieClip; 
import flash.events.*; 

stage.frameRate = 31; 
var a =0; 

btn111.addEventListener(MouseEvent.CLICK, go); 

function go(event:MouseEvent):void 
{ 
    this.addEventListener(Event.ENTER_FRAME, drawrect); 

    function drawrect(evt:Event) 
    { 
     // Create a new instance of the Loader class to work with 
     var myLoader:Loader=new Loader(); 

     // Create a new URLRequest object specifying the location of the external image file 
     var myRequest:URLRequest=new URLRequest("logo.png"); 

     // Call the load method and load the external file with URLRequest object as the parameter 
     myLoader.load(myRequest); 

     // Add the Loader instance to the display list using the addChild() method 
     addChild(myLoader); 

     // Position image 
     myLoader.x = 100; 
     myLoader.y = a++; 

     if(a > 50) 
     { 
      //removeChild(box); 
      removeEventListener(Event.ENTER_FRAME, drawrect); 
     } 
    } 
} 

回答

0

您的问题是,每一帧上,你实际上是添加新的子显示列表 - 你实际上并没有移动一个对象,但在不同的位置加载多个对象。你需要将你的loader加载到另一个不能每帧运行的函数中,或者你需要将它封装在一个if块中,检查它是否存在。

试试这个。

import flash.display.MovieClip; 
import flash.events.*; 

stage.frameRate = 31; 
var a =0; 

private var myLoader:Loader; 

btn111.addEventListener(MouseEvent.CLICK, go); 

function go(event:MouseEvent):void 
{ 
    this.addEventListener(Event.ENTER_FRAME, drawrect); 

    function drawrect(evt:Event) 
    { 
     if (myLoader == NULL) 
     { 
      // Create a new instance of the Loader class to work with 
      myLoader:Loader=new Loader(); 

      // Create a new URLRequest object specifying the location of the external image file 
      var myRequest:URLRequest=new URLRequest("logo.png"); 

      // Call the load method and load the external file with URLRequest object as the parameter 
      myLoader.load(myRequest); 

      // Add the Loader instance to the display list using the addChild() method 
      addChild(myLoader); 
     } 

     // Position image 
     myLoader.x = 100; 
     myLoader.y = a++; 

     if(a > 50) 
     { 
      //removeChild(box); 
      removeEventListener(Event.ENTER_FRAME, drawrect); 
     } 
    } 
} 

此外,我会建议对函数的函数内(除非你真的知道你希望它是这样.....但你可能不希望它是这样)。

祝你好运。