// you’re reading...

ActionScript 3.0

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.

Discussion

11 comments for “Flash/Flex Integration: SWFLibrary v1.5”

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

    Posted by Benjamin | August 21, 2008, 1:34 pm
  2. Thanks to share this source! It saves me!

    Posted by LutinCapuche | August 21, 2008, 5:07 pm
  3. Nice! I extended it with EmbedSWFLibrary. Especially useful for single-file haXe apps.

    Posted by Gust | August 22, 2008, 6:57 am
  4. […] 来自 […]

    Posted by iTamt.cn » 博客文章 » SWFLibrary v1.5 | August 22, 2008, 10:31 pm
  5. How come your site is redirected from ErinGipson.com?

    Posted by j | September 11, 2008, 10:37 am
  6. 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.

    Posted by Zack Jordan | September 11, 2008, 10:46 am
  7. […] thanks to Zack Jordan for sharing his SWFLibrary […]

    Posted by PV3D Page Flip with AS3Dmod and Effects | publicreative lab | January 13, 2009, 1:53 pm
  8. thanks for the great ideas, I’ve been scouring the web and blogs for Flex game development ideas - thanks again!

    Posted by John Kennedy | February 5, 2009, 4:07 pm
  9. 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);
    }
    }

    }

    Posted by syamsundar | February 27, 2009, 5:12 am
  10. 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()

    Posted by adampasz | August 28, 2009, 10:10 am
  11. 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.

    Posted by Zack Jordan | August 28, 2009, 10:21 am

Post a comment