Jump to content
Search Community

Loading shared assets

friendlygiraffe test
Moderator Tag

Recommended Posts

Hello - I'm using LoaderMax a lot now, but wondered if there was a way of using it to control shared assets at runtime?:

 

Here is an example of external asset loading:

 

http://old-wp.slekx.com/2010/01/flash-c ... -tutorial/

 

package {

   import flash.display.*;
   import flash.events.*;
   import flash.net.*;
   import flash.system.ApplicationDomain;
   import flash.system.LoaderContext;
   import flash.utils.getDefinitionByName;

   public class Project extends Sprite{

       public function Project(){
           // this context is necessary to find the shared assets
           var context:LoaderContext = new LoaderContext(false,
                                       ApplicationDomain.currentDomain);

           // load in the asset swf
           var loader:Loader = new Loader();
           var req:URLRequest = new URLRequest("Assets.swf");
           loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
                                                     onAssetsLoaded);
           try{
               loader.load(req, context);
           }catch(e:Error){
               trace("Asset load error: " + e);
           }
       }

       private function onAssetsLoaded(_event:Event):void{
           // get a reference to the loaded library
           var loader:Loader = LoaderInfo(_event.target).loader;
           var library:* = loader.content;

           var offsetX:Number = 0;

           // loop through each asset defined in the library
           // using the its 'availableGraphics' property.
           for each(var asset:String in library.availableGraphics){
               // get the MovieClip class of the asset
               var assetClass:Class = getDefinitionByName(asset) as Class;

               // create an instance of the shared asset
               var sharedAsset:DisplayObject = new assetClass();

               // position the asset
               sharedAsset.x = offsetX + 10;
               sharedAsset.y = 10;

               // add asset to the stage
               addChild(sharedAsset);

               // update position
               offsetX += sharedAsset.width + 10;
           }
       }

   }
}




 

Thanks

Link to comment
Share on other sites

once you load your assets.swf with a SWFLoader you should be able to access objects in it's library with

 

getClass()

 

http://www.greensock.com/as/docs/tween/ ... #getClass()

How would I get the library MovieClip 'mc' from this code?:

 

var queue:LoaderMax = new LoaderMax({name:"mainQueue", onComplete:completeHandler});
queue.append( new SWFLoader("Assets.swf", {name:"Assets", estimatedBytes:4351877, container:this, autoPlay:false}));

public function completeHandler(event:LoaderEvent):void {

var testClass:Class = queue.getClass("mc");

}

Link to comment
Share on other sites

Ok, I'm nearly there. I just need to figure out how to add the Library asset 'tractor_frames' to the stage, addChild() doesn't seem to work. Do I need to add it to the DisplayObject ?

 

package 
{
import flash.display.*;
import flash.events.*;

import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.display.*;

public class Tractor extends MovieClip
{

	var queue:LoaderMax = new LoaderMax({name:"mainQueue",onComplete:completeHandler,onError:errorHandler});

	public function Tractor()
	{
		queue.append( new SWFLoader("Assets.swf", {name:"Assets", estimatedBytes:4351877, container:this, autoPlay:true}) );
		queue.load();
	}

	public function completeHandler(event:LoaderEvent):void
	{
		var childLoader:SWFLoader = queue.getLoader("Assets");
		var librarySymbol:Class = childLoader.getClass("tractor_frames");
		trace(librarySymbol);
	}

	public function errorHandler(event:LoaderEvent):void
	{
		trace("error occured with " + event.target + ": " + event.text);
	}
}
}

Link to comment
Share on other sites

there is one more step:

 

 

 

 public function completeHandler(event:LoaderEvent):void
     {
        var childLoader:SWFLoader = queue.getLoader("Assets");


        var librarySymbol:Class = childLoader.getClass("tractor_frames");
        trace(librarySymbol);

//this will create an instance of your class
      var myInstance:MovieClip = new librarySymbol();
addChild(myInstance)

     }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...