FlashLib – Using Flash Library assets in Flex2 applications

[kml_flashembed movie=”http://www.acmewebworks.com/Images/blog/FlashLibDemo/FlashLibDemo.swf” width=”600″ height=”300″/]

Grant Skinner just posted his FlashLib extension for the Flash 9 alpha IDE / Flash CS3 with a great explanation of how it prepares your library for use in Flex2. He calls em’ "Shake n’ Bake SWF’s", I call em’ buiscuits of joy! Ok enough with the Southern references🙂

Why do I think this is SO friggin’ cool?? Because it allows me to VERY easily work with my MovieClip textures for my Papervision3D models – THAT’S WHY.

The Xwing above, which we’ve seen on several occasions, now uses a MovieClip ( MovieMaterial ) for the skin rather than an externally loaded JPG or embedded bitmap within the compiled Flex2 app. For this demo, you’ll see that I’ve added some really simple animations. Engine Glows on the back and an orange dash that flows along the body in the front. The orange dash also has a Glow filter applied😉

Now the workflow is rediculously cool. Modify the texture as a MovieClip in the Flash IDE like any other day, use the new FlashLib Create command in the commands list and bam – I have a FlashLib.swf and a FlashLib.as class to compile with my Flex2 app.

Usage in Flex2 is straight forward:

[as]
package
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;

import mx.containers.Canvas;

import org.papervision3d.core.utils.Collada;
import org.papervision3d.events.FileLoadEvent;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.MaterialsList;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.scenes.Scene3D;
import org.paperworld.events.ObjectReadyEvent;
import org.paperworld.objects.PaperworldCollada;

public class ColladaSceneLoader extends EventDispatcher
{
public static var COLLADA_LOADED:String = “xwingLoaded”;

public var container:Canvas;
public var file:String;
public var scene:Scene3D;
public var texture:DisplayObject;
public var xwingTexture:MovieMaterial;

private var collada:PaperworldCollada;
private var matsList:MaterialsList = new MaterialsList();
private var timer:Timer = new Timer(10,0);

public function ColladaSceneLoader(target:IEventDispatcher=null)
{
super(target);
}

public function getObject():DisplayObject3D
{
return collada.getObj();
}

public function init(p_file:String, p_scene:Scene3D):void
{
file = p_file;
scene = p_scene;
FlashLib.eventDispatcher.addEventListener(Event.INIT, handleImageInit);
}

private function loadCollada():void
{
collada = new PaperworldCollada(“xwingContainer”,scene, matsList, file, .0012);
collada.addEventListener(ObjectReadyEvent.OBJECT_READY, handleColladaLoadComplete);
}

private function handleBitmapLoaded(e:FileLoadEvent):void
{
loadCollada();
}

private function handleImageInit(e:Event):void
{
texture = FlashLib.getInstance(“mainSkin”);
xwingTexture = new MovieMaterial(texture);
xwingTexture.oneSide = true;
matsList.addMaterial(xwingTexture, “xwingTexture”);

loadCollada();
}

private function handleTimerUpdate(e:TimerEvent):void
{
xwingTexture.updateBitmap();
}

private function handleColladaLoadComplete(e:ObjectReadyEvent):void
{
timer.addEventListener(TimerEvent.TIMER, handleTimerUpdate);
timer.start();
dispatchEvent(new Event(COLLADA_LOADED));
}
}
}
[/as]

In the init for the class, I call

[as]FlashLib.eventDispatcher.addEventListener(Event.INIT, handleImageInit); [/as]

When the INIT event fires handleImageInit creates the instance using FlashLib and then goes on normally to create the MovieMaterial object that can be added to the materials list for the COLLADA scene:

[as]private function handleImageInit(e:Event):void
{
texture = FlashLib.getInstance(“mainSkin”);
xwingTexture = new MovieMaterial(texture);
xwingTexture.oneSide = true;
matsList.addMaterial(xwingTexture, “xwingTexture”);

loadCollada();
}[/as]

And, as usual, this was JUST in time for something I’m cooking up for FITC – Grant’s timing couldn’t have been more perfect as this little gem brought it all together🙂

Check it out over on Grant’s blog:
http://www.gskinner.com/blog/archives/2007/03/using_flash_sym.html

RockOn Rating:   RockOn voteRockOn voteRockOn vote

    • Corey
    • May 1st, 2007

    Johnny Royale!!!!!!!!!!!!!!!!!!

  1. COREY!!!!!! Where you been man!?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s