Monday, July 23, 2012

How to run an external SWF inside a Flex Application?

Question:

How to run an external SWF inside a Flex Application?

 
The problem is that in the calling of swfApplication.init(); the AIR Player throws me an exception: 
Security sandbox violation: caller file:///path/to/the/application.swf cannot access Stage owned by app:/SWFApplicationLoader.swf.
This is because somewhere in application.swf I use the stage like this:
if (root.stage != null)
    root
.stage.addEventListener(Event.REMOVED, someFunction);
root
.stage.stageFocusRect = false;
How can I load this swf application and USE the stage without any problems?

 
 
You can try to load your SWF temporarily into a ByteArray and then load it with your SWFLoader.
Don't forget to set allowLoadBytesCodeExecution to true since your SWF has as code inside.
Of course be sure that your loaded swf is secure enough for your application since it will have access at all your property.
 
private function loadSwfApplication():void {
  // load the file with URLLoader into a bytearray
  var loader:URLLoader=new URLLoader();

  // binary format since it a SWF
  loader.dataFormat=URLLoaderDataFormat.BINARY;
  loader.addEventListener(Event.COMPLETE, onSWFLoaded);

  //load the file
  loader.load(new URLRequest("path/to/the/application.swf"));
}
private function onSWFLoaded(e:Event):void {
 // remove the event
 var loader:URLLoader=URLLoader(e.target);
 loader.removeEventListener(Event.COMPLETE, onSWFLoaded);

 // add an Application context and allow bytecode execution 
 var context:LoaderContext=new LoaderContext();
 context.allowLoadBytesCodeExecution=true;

 // set the new context on SWFLoader
 sfwLoader.loaderContext = context;

 sfwLoader.addEventListener(Event.COMPLETE, loadComplete);

 // load the data from the bytearray
 sfwLoader.load(loader.data);
}
// your load complete function
private function loadComplete(completeEvent:Event):void {
 var swfApplication:* = completeEvent.target.content;
 swfApplication.init();  // this is a Function that I made it in the Root 
                         // class of swfApplication
}
 
 source: 

Monday, June 6, 2011

scaleContent property is true, but the UILoader is not resized ?

Resizes the component to the requested size. If the scaleContent property is set to true, the UILoader is not resized.



import fl.containers.UILoader;

var myUILoader:UILoader = new UILoader();
myUILoader.source = "http://www.helpexamples.com/flash/images/image2.jpg";
myUILoader.addEventListener(Event.COMPLETE, completeHandler);
addChild(myUILoader);

function completeHandler(event:Event):void {
var uiLdr:UILoader = event.currentTarget as UILoader;
var image:DisplayObject = uiLdr.content as DisplayObject;
trace("UILoader:", uiLdr.width, uiLdr.height); // 100 100
trace("UILoader.content:", image.width, image.height); // 400 267
uiLdr.setSize(image.width, image.height);
uiLdr.move((stage.stageWidth - image.width) / 2, (stage.stageHeight - image.height) / 2);
}

Wednesday, September 8, 2010

Adding a movieclip to a scrolling component list!

a solution for adding a movieclip to a scrolling component list!

http://livedocs.adobe.com/flash/9.0/...html#iconField

Check out the entry on "IconField". Note that in the example, RedBox would be the instance name of your MovieClip.

Here's the example it gives:

The following example creates a list with an icon for each item. A symbol named RedBox must exist in the library and have "Export for ActionScript" toggled in its symbol properties:

PHP Code:
import fl.data.DataProvider;
import fl.controls.List;

var
dp:DataProvider = new DataProvider();
dp.addItem( { iconSource:RedBox, label:"Item 1" } );
dp.addItem( { iconSource:RedBox, label:"Item 2" } );
dp.addItem( { iconSource:RedBox, label:"Item 3" } );

var list:List = new List();
list.
iconField = "iconSource";
list.
dataProvider = dp;
addChild(list);