Search code examples
flashactionscript-3programming-languagesloader

How to use a new movieClip as a Class or to copy it without reference to its class?


I am doing a looping background with 2 identical BG movieClip (B1 and B2). Those 2 are "new" by the same class.

var B1:MovieClip = new BG();
var B2:MovieClip = new BG();

But because this BG class picture is loader from the internet and the picture is rather big. When I addressed its loading by using progressEvent. The picture is really loaded 2 times. Is there anyway I create B2 based on B1 without using new BG() again? Since it definitely makes a new BG with Loader code inside. Thank you for your help ^^

For BG class code:

var URLvar:URLRequest;
var BGLoader : Loader = new Loader( );
URLvar = new URLRequest( "somewherelink.jpg" );
BGLoader.load(URLvar);
this.addChild(BGLoader);

is there any way that I set if the B1 is completely loaded, I copy it into B2? or any other way to achieve the same goal?

import flash.display.*;
import flash.utils.*;
var obj:Class = getDefinitionByName(“bluebox”) as Class;
addChild(MovieClip(new obj()));

I found this one in a dynamically Create Class, but I dunno how to use T_T, can you explain it to me and can I use it in this situation? Thank you for your time ^^


Solution

  • For this answer I'm going to assume that you are always going to want to have the same image twice, rather than two different ones. The problem you have is that your BG class does the load and the attach completely internally. My preferred approach would be to load once, and then create to instances of the loaded content.

    //first lets create a single loader
    var bgLoader:Loader = new Loader();
    bgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
    //and load the content
    var urlVar:URLRequest = new URLRequest("somewherelink.jpg");
    bgLoader.load(urlVar);
    //notice that we don't add the loader to the stage
    
    function completeHandler(evt:Event):void
    {
        //remove the loader listener for memory conservation purposes
        evt.target.removeEventListener(Event.COMPLETE,completeHandler);
        //reference the content
        var content:DisplayObject = evt.target.content;
        //so now create a bitmapData object
        var bmd:BitmapData = new BitmapData(content.width,content.height,false); // use (content.width,content.height,true,0) if loading image with transparancy
        //and draw the loaded image into to bitmap data
        bmd.draw(content);
        //now you can create as many Bitmap objects from this data as you like
        var bg1:Bitmap = new Bitmap(bmd);
        var bg2:Bitmap = new Bitmap(bmd);
        //offset bg2 so both are visible
        bg2.x = 100;
        addChild(bg1);
        addChild(bg2);
    }
    

    For your second question... getDefinitionByName returns you an object that represents a named class, rather than an instance of it. So in your example, it is getting a definition for the class 'bluebox', which is in the root of your application source (not in a folder).

    Once you have that class object you can create instances of it with the new keyword. The big disadvantage of this as far as I'm concerned is that you don't import the class, hence in your example obj is cast as a MovieClip. That makes features of code editors like code completion and error highlighting to not work correctly because the properties and methods of 'bluebox' are not exposed.

    I usually only use this for creating objects that will have their classes defined elsewhere, in an XML file that is loaded at runtime for example.