this is portable

Papervision2.0 - dynamically loading MovieMaterial in Flex

August 16th, 2008

flash

I've been working with Flex Builder a lot more lately in case you haven't noticed. I've noticed that a lot Papervision2.0 examples I run into that use MovieMaterial embed the source .swf a little like this:

Actionscript:
  1. [Embed(source="img01.jpg")]

But, what about loading the .swf in dynamically for more flexibility? It's a whole lot easier than you think:

Actionscript:
  1. package {
  2. import flash.display.Loader;
  3. import flash.display.MovieClip;
  4. import flash.events.Event;
  5. import flash.events.IOErrorEvent;
  6. import flash.events.ProgressEvent;
  7. import flash.geom.Rectangle;
  8. import flash.net.URLRequest;
  9.  
  10. import org.papervision3d.materials.MovieMaterial;
  11. import org.papervision3d.objects.primitives.Plane;
  12. import org.papervision3d.view.BasicView;
  13.  
  14. public class FlexLoader extends BasicView
  15. {
  16. private var request:URLRequest;
  17. private var loader:Loader;
  18.  
  19. public function FlexLoader()
  20. {
  21. super(200,200,false, true);
  22. loadSWF();
  23. startRendering();
  24.  
  25. }
  26.  
  27. private function loadSWF():void {
  28. request = new URLRequest("assets/matTest.swf");
  29. loader = new Loader();
  30.  
  31. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
  32. loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError);
  33. loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
  34. loader.contentLoaderInfo.addEventListener(Event.INIT, loadInit);
  35.  
  36. loader.load(request);
  37.  
  38. }
  39.  
  40. private function makePlane(e:Event):void {
  41.  
  42. //create material
  43. var loadedVidSWF:MovieClip = e.target.content as MovieClip;
  44. var loadedMat:MovieMaterial = new MovieMaterial(loadedVidSWF, false, true, false, new Rectangle(0,0,400,400));
  45. loadedMat.doubleSided = true;
  46. loadedMat.animated = true;
  47.  
  48. //create plane
  49. var vidPlane:Plane = new Plane(loadedMat,400,400,5,5);
  50. scene.addChild(vidPlane);
  51. }
  52.  
  53. private function loadComplete(e:Event):void {
  54. trace(e);
  55. }
  56.  
  57. private function loadError(e:IOErrorEvent):void {
  58. trace(e);
  59. }
  60.  
  61. private function loadProgress(e:ProgressEvent):void {
  62. trace(e);
  63. }
  64.  
  65. private function loadInit(e:Event):void {
  66. trace(e);
  67. makePlane(e);
  68. //to call a function on your SWFs timeline
  69. //e.target.content.yourFunction(args);
  70.  
  71. }
  72.  
  73. override protected function onRenderTick(event:Event = null):void
  74. {
  75. super.onRenderTick(event);
  76. }
  77.  
  78. }
  79.  
  80. }

Here's the source for even easier reading. It uses rev.691 of Papervision2.0 GreatWhite, but the Papervision classes are not included in this zip file.

Leave a Comment

your name and email are required to post a comment. however, your email will NOT be published.