![]()
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.
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:
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.
Here are the main differences from the last version:
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.
So have fun with this. Hopefully it saves you as much time as it saves me.
That’s really quite helpful and solid. Thanks, Zack!
Thanks to share this source! It saves me!
Nice! I extended it with EmbedSWFLibrary. Especially useful for single-file haXe apps.
[…] 来自 […]
How come your site is redirected from ErinGipson.com?
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.
[…] thanks to Zack Jordan for sharing his SWFLibrary […]
thanks for the great ideas, I’ve been scouring the web and blogs for Flex game development ideas - thanks again!
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);
}
}
}
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()
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.