Flash/Flex Integration: SWFLibrary v1.5

Usually I like to post glamorous stuff, like 3D swirly things and experiments. Occasionally, however, I feel the need to release a little code in the interest of helping a few people out. This code is a review of sorts, since I already posted a similar utility back in June. However, since then I have been expanding my code library and I thought I’d share the latest and much improved version. It’s really a simple concept, but having this in my toolbox has lightened my workload as an ActionScripter considerably.

Advantages

This utility allows you to publish any symbol from Flash and access it at runtime from an ActionScript-only project, such as in Flex Builder. This has a few advantages:

  • Easy storage of many assets in one file
  • Ability to change graphics without recompiling the main application
  • Faster load times with only one HTTP request (as opposed to many separate images and sounds)
  • Ability to import vector graphics and animations at runtime
  • Smaller main SWF size

The one disadvantage that I can think of is that you’ll need to recompile your library SWF every time you make a change. However, I think the advantages above outweigh this.

Differences in v1.5

Here are the main differences from the last version:

  • The class is no longer static. This allows for more than one instance of SWFLibrary at a time.
  • The class can now return instances of Sprites, MovieClips, and Sounds from a library SWF.

Usage

1 - In Flash, select “Export for ActionScript” in the symbol’s Linkage settings in the Library (ex. ‘Character’).
2 - Publish the Flash file as a SWF.
3 - Load the SWF into an instance of SWFLibrary
4 - Access the symbol through SWFLibrary, using the class name chosen in Step 1.

A SWFLibrary can be used with four lines of code. Here’s what you’ll need:

// creates an instance of SWFLibrary, adds a listener, and loads a SWF
var gameAssets:SWFLibrary = new SWFLibrary
gameAssets.addEventListener( Event.COMPLETE, handleAssetsLoaded );
gameAssets.load( "myCustomGameAssets.swf" );
 
// accesses the asset (assumes there is a symbol exported as 'Character' in the loaded SWF, as seen in step 1 above)
// this code should run after the Event above has fired
var mySprite:Sprite = gameAssets.getSprite( "Character" );
addChild( mySprite );
 
// gets the asset as a MovieClip (assuming, of course, that it actually is a movieclip)
var myMC:MovieClip = gameAssets.getSprite( "Character" );
addChild( myMC );
 
// getting a sound
var mySound:Sound = gameAssets.getSound( "SoundLinkageNameHere" );
mySound.play();
// === ===

After a symbol has been retrieved from SWFLibrary, it may be treated like any other object.

Download

DOWNLOAD SOURCE

So have fun with this. Hopefully it saves you as much time as it saves me.

Tags: ,

13 Responses to “Flash/Flex Integration: SWFLibrary v1.5”

  1. Benjamin Says:

    That’s really quite helpful and solid. Thanks, Zack!

  2. LutinCapuche Says:

    Thanks to share this source! It saves me!

  3. Gust Says:

    Nice! I extended it with EmbedSWFLibrary. Especially useful for single-file haXe apps.

  4. iTamt.cn » 博客文章 » SWFLibrary v1.5 Says:

    […] 来自 […]

  5. j Says:

    How come your site is redirected from ErinGipson.com?

  6. Zack Jordan Says:

    I used to host her site until she took it down. I guess her domain still points to my server.

    Kinda interesting though. I’ll have to look into it.

  7. PV3D Page Flip with AS3Dmod and Effects | publicreative lab Says:

    […] thanks to Zack Jordan for sharing his SWFLibrary […]

  8. John Kennedy Says:

    thanks for the great ideas, I’ve been scouring the web and blogs for Flex game development ideas - thanks again!

  9. syamsundar Says:

    package
    {
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.events.MouseEvent;

    import org.papervision3d.core.geom.renderables.Vertex3D;
    import org.papervision3d.core.math.Number3D;
    import org.papervision3d.core.math.Plane3D;
    import org.papervision3d.core.math.Quaternion;
    import org.papervision3d.lights.PointLight3D;
    import org.papervision3d.materials.BitmapMaterial;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.Sphere;
    import org.papervision3d.view.BasicView;

    [SWF(width=”640″, height=”480″, backgroundColor=”#000000″, frameRate=”60″)]
    public class QuaternionExample extends BasicView
    {
    private const XYPLANE:Number3D = new Number3D(0, 0, 1);
    private const RADIUS:Number = 400;
    private const SPEED:Number = .05;

    private var plane3D:Plane3D = new Plane3D(XYPLANE, Number3D.ZERO);;
    private var sphere:Sphere;

    [Embed(source=”assets/lake_640.jpg”)]
    private var materialAsset:Class;

    public function QuaternionExample()
    {
    var h:headerContainer = new headerContainer();
    h.header.text = “Move Mouse to Rotate, Click to Reset”;
    addChild(h);

    var light:PointLight3D = new PointLight3D();
    var bitmap:Bitmap = new materialAsset() as Bitmap;
    var material:BitmapMaterial = new BitmapMaterial(bitmap.bitmapData);
    sphere = new Sphere(material, RADIUS, 30, 30);

    scene.addChild(sphere);

    startRendering();

    stage.addEventListener(MouseEvent.CLICK, clickHandler);
    }

    private function clickHandler(event:MouseEvent):void
    {
    //quick reset
    sphere.copyTransform(new DisplayObject3D());
    }

    override protected function onRenderTick(event:Event=null):void
    {
    var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
    ray = Number3D.add(ray, camera.position);

    var cameraVertex3D:Vertex3D = new Vertex3D(camera.x, camera.y, camera.z);
    var rayVertex3D:Vertex3D = new Vertex3D(ray.x, ray.y, ray.z);

    var intersectPoint:Vertex3D = plane3D.getIntersectionLine(cameraVertex3D, rayVertex3D);

    var velocityX:Number = intersectPoint.x * SPEED;
    var velocityY:Number = intersectPoint.y * SPEED;
    var velocityZ:Number = intersectPoint.y * SPEED;

    var difference:Number3D = new Number3D(-velocityX, -velocityY, -velocityZ);
    var distance:Number = Math.sqrt(difference.x * difference.x + difference.y * difference.y);

    var rotationAxis:Number3D = Number3D.cross(difference, XYPLANE);
    rotationAxis.normalize();

    var rotation:Quaternion = Quaternion.createFromAxisAngle(rotationAxis.x, rotationAxis.y, rotationAxis.z, distance/RADIUS);
    rotation.normalize();

    sphere.transform.calculateMultiply3×3(rotation.matrix, sphere.transform);

    renderer.renderScene(scene, camera, viewport);
    }
    }

    }

  10. adampasz Says:

    Amazing! Thank you!

    I’ve been looking for this functionality for years. Up to this point I’ve relied on a hacky solution that required artists to add 2 lines of code to all their FLAs that retrieved the symbol.

    Now maybe someone can explain why Adobe buried this critical functionality 3 levels deep in Loader.loaderInfo.applicationDomain.getDefinition()

  11. Zack Jordan Says:

    No problem. Actually, I’ve got a v2.2 that I’m about ready to release. If you want to use this for a project, I’ll send it to you to test-drive.

  12. sudaka Says:

    Thanks a lot for your swfLibrary ….
    I add a function for the font LOADING, i don’t know if this is the right path or solution :::::

    in SWFLibrary.as I add:
    public function getFont( className:String )
    {
    var Asset:Class = _loaderInfo.applicationDomain.getDefinition( className ) as Class;
    return Asset;
    }

    //////for call the function/////
    // creates an instance of SWFLibrary, adds a listener, and loads a SWF
    var gameAssets:SWFLibrary = new SWFLibrary
    gameAssets.addEventListener( Event.COMPLETE, handleAssetsLoaded );
    gameAssets.load( “myCustomGameAssets.swf” );

    // getting a sound
    var font:Class = gameAssets.getFont( “FontLinkageNameHere” );
    Font.registerFont(font);

    ///////////////////////
    Can you say me if this is the good solution, Thaks a lot for your reponse

  13. SWFLibrary类加载外部资源库 - 33ue Says:

    […] 原始链接:http://pixelwelders.com/blog/best-practices/2008/flashflex-integration-swflibrary-v15/ RelatedFlash,Flash AS,TextField对象相关的属性配置FlashDevelop软件并支持Flash CS4[转] Feed me       […]

Leave a Reply