0
votes

I'm trying to load a bitmap to the stage then tween it across entirely using AS code. The following works but when it adds a new bitmap image to the stage it leaves the last one thus leaving a load of the same bitmap.

Any ideas? I tried adding "removeChild(myLoader);" but that did nothing. Many thanks.

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);
        }
    }
}
1

1 Answers

0
votes

Your problem is that, on every frame, you're actually adding a new child to the display list - you aren't actually moving one object, but loading multiple objects at different positions. You either need to move your loader into another function which doesn't run per-frame, or you need to encapsulate it in an if-block that checks whether it exists or not.

Try this.

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);
        }
    }
}

Additionally, I would advise against functions within functions (unless you really know that you want it to be that way.....but you probably don't want it to be that way).

Good luck.