Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Paste Description for Henke37

The workaround for embedding swf files in flex.

Henke37
Tuesday, July 1st, 2008 at 4:04:02pm MDT 

  1. package HTools {
  2.         import flash.display.Sprite;
  3.         import flash.display.MovieClip;
  4.         import flash.display.Loader;
  5.         import flash.events.Event;
  6.         import flash.utils.ByteArray;
  7.        
  8.         import mx.core.ByteArrayAsset;
  9.  
  10.         public class SwfLoader extends Sprite {
  11.                
  12.                 /**
  13.                  * Swf loader class, wraps the loading of a bytearray from an embeded symbol
  14.                  * Copyright Henke37 2008
  15.                  * Just give credit when it's due.
  16.                  */
  17.                 
  18.                  /*
  19.                         Here is how to use this class:
  20.                         # Create the fla
  21.                         # Set the publish settings of the fla to NOT automaticaly declare instances
  22.                         # Set and write the document class for the FLA. It must be declared public!
  23.                         # Publish the fla to get the swf
  24.                         # Embed the swf, but mark it as having the mimeType "application/octet-stream".
  25.                         # Create an asset ByteArray by calling new on the asset property
  26.                         # Create an instance of the Swfloader class (that's this class),
  27.                                passing the ByteArray from the previous step
  28.                         # Write an even handler for the Event.COMPLETE event that is fired from the loader.
  29.                                 In that, you can access the content property of this class.
  30.                                 Do not forget to cast the object to the document class
  31.                         # Add the event listener to the loader. DO NOT idle with this,
  32.                                 do it ASAP, like directly after creating it.
  33.                                 Otherwise you might miss the event.
  34.                                
  35.                         The gotcha list:
  36.                         * Not waiting for the Event.COMPLETE event before using the content property
  37.                         * Not declaring the document class to be public
  38.                         * Not marking the embed tag with the proper mimeType
  39.                         * Forgeting to store the content object in a variable with type of the Document class
  40.                         * Not declaring the instances explitly in the document class,
  41.                                 preventing Flex from finding the properties for them
  42.                                
  43.                         Also, try to keep the documet class small since it will be embeded twice.
  44.                  */
  45.                
  46.                 private var loader:Loader;
  47.                 private var data:ByteArray;
  48.                
  49.                 public function SwfLoader(data:ByteArray) {
  50.                         super();
  51.                         loader=new Loader();
  52.                         loader.loadBytes(data);
  53.                         addEventListener(Event.ENTER_FRAME,onEnterFrame,false,0,true);
  54.                 }
  55.                
  56.                 public function get content ():MovieClip {
  57.                         if(loader.content) {
  58.                                 return MovieClip(loader.content);
  59.                         } else {
  60.                                 return null;
  61.                         }
  62.                 }
  63.                
  64.                 private function onEnterFrame(e:Event):void {
  65.                         if(loader.content) {
  66.                                 dispatchEvent(new Event(Event.COMPLETE));
  67.                                 removeEventListener(Event.ENTER_FRAME,onEnterFrame);
  68.                         }
  69.                 }
  70.                
  71.         }
  72. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

fantasy-obligation