Ask a Question related to Macromedia Flex General Discussion, Design and Development.
-
jpsoul #1
Type Casting Loaded Swf
I'm having trouble type casting an MXML created swf as in I can't seem to get
it to work.
Here is what I'm doing:
1. Created a LoaderTest.swf that loads AppToLoad.swf
2. Made AppToLoad implement ISomeInterface
3. Once AppToLoad.swf is loaded in LoaderTest.swf, attempt to type
cast it as ISomeInterface
Code (all code is in the same root package):
<!-- Begin ISomeInterface -->
package
{
public interface ISomeInterface
{
function doSomething():void;
}
}
<!-- End ISomeInterface -->
<!-- Begin AppToLoad -->
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" implements="ISomeInterface">
<mx:Script>
<![CDATA[
private var myInterface:ISomeInterface;
public function doSomething():void
{
trace("doSomething");
}
]]>
</mx:Script>
</mx:Application>
<!-- End AppToLoad -->
<!-- Begin LoaderTest -->
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" applicationComplete="start()">
<mx:Script>
<![CDATA[
import mx.managers.SystemManager;
private var ldr:Loader;
private function start():void
{
ldr = new Loader();
var ldrContext:LoaderContext = new
LoaderContext(false,
ApplicationDomain.currentDomain);
ldr.contentLoaderInfo.addEventListener(Event.INIT, handleLoaded);
ldr.load(new URLRequest("AppToLoad.swf"),
ldrContext);
}
private function handleLoaded(e:Event):void
{
trace("handleLoaded");
var content:Object =
LoaderInfo(e.target).content;
trace("content = "+content);
var typedContent:ISomeInterface = content as
ISomeInterface;
trace("typedContent = "+typedContent);
var typedApplication:ISomeInterface = (content
as
SystemManager).application as ISomeInterface;
trace("typedApplication = "+typedApplication);
}
]]>
</mx:Script>
</mx:Application>
<!-- End LoaderTest -->
Output from running LoaderTest:
[SWF] C:\EDGE\testFolder\bin-debug\LoaderTest.swf - 555,437 bytes
after decompression
[SWF] C:\EDGE\testFolder\bin-debug\AppToLoad.swf - 554,844 bytes after
decompression
handleLoaded
content = [object _AppToLoad_mx_managers_SystemManager]
typedContent = null
typedApplication = null
Any idea what I'm doing wrong?
-JP
jpsoul Guest
-
#39680 [NEW]: Type Casting from float into int
From: diefans at googlemail dot com Operating system: Linux PHP version: 5.2.0 PHP Bug Type: *Math Functions Bug... -
web casting
hi i'm looking for ext for DW for webcasting (appli with mysql-jsp and multimedia courses) -
Ray Casting
Is there an easy way to keep 2 objects the same distance from each other? I want the camera to cast a ray downward and move the camera up or down to... -
CASTING
How could I compare two fields one of which is of integer type, the other is char? The version of the INFORMIX is 7.2. Thanks in advance. -
Type casting...
Is it better coding practice to do this: $wibble = (bool)true; $wobble = (int)108; or just this: $wibble = true; $wobble = 108; -
slaingod #2
Re: Type Casting Loaded Swf
I'd be interested in seeing the solution as well, but a quick workaround is to
cast it to the unbind operator.
This is how I've always done it... Maybe you can get it to work with
implements:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.managers.SystemManager;
import mx.core.Application;
// * gets rid of type checking so we can leave strict enabled
[Bindable]public var _appLoaded:*;
private var load_finish_playlist:int = 0;
private var load_finish_name:String = "";
private function onCurrentApplicationComplete(oEvent:Event):void
{
_appLoaded = Application(oEvent.target.application);
if(load_finish_playlist > 0) {
setPlaylist(load_finish_playlist);
}
if(load_finish_name != "") {
setPlayerName(load_finish_name);
}
}
private function onCompleteAppLoader(oEvent:Event):void
{
var smAppLoaded:SystemManager = SystemManager(oEvent.target.content);
//get a ref to the loaded app
//listen for the application.complete event
smAppLoaded.addEventListener(FlexEvent.APPLICATION _COMPLETE,
onCurrentApplicationComplete);
}
public function setPlaylist(id:int):void {
if(_appLoaded) {
_appLoaded.change_playlist(id);
} else {
load_finish_playlist = id;
}
}
public function setPlayerName(name:String):void {
if(_appLoaded) {
_appLoaded.set_name(name);
} else {
load_finish_name = name;
}
}
public function stopPlayback():void {
_appLoaded.stop_playback();
}
public function startPlayback():void {
_appLoaded.start_playback();
}
]]>
</mx:Script>
<!-- <mx:SWFLoader id="player" x="0" y="0"
source="http://...../flash/mixplayer3.swf"
complete="onCompleteAppLoader(event);" trustContent="true"/>
--> <mx:SWFLoader id="player" x="0" y="0"
source="@Embed('../assets/Player.swf')" complete="onCompleteAppLoader(event);"
trustContent="true"/>
</mx:Canvas>
slaingod Guest
-
jpsoul #3
Re: Type Casting Loaded Swf
yeah, what i really want is to be able to call the api of my loaded swf with
strict compiling. i'm not sure if your workaround satisfies that. interesting
idea though to add an event handler for the application complete of the loaded
swf. i haven't been able to get that to work as your example suggests (since I
don't have your assets I can't compile your example).
there has to be a way to do this.
jpsoul Guest
-
jpsoul #4
Re: Type Casting Loaded Swf
Okay, I think I figured it out.
In order to type cast a loaded mxml compiled swf to an interface that it
implements:
#1. You need to wait for the applicationComplete event to occur before type
casting.
#2. Use the SWFLoader instead of a Loader as the Loader (or more precisely,
the LoaderInfo object of the Loader) will not dispatch applicationComplete
events.
This worked for me.
jpsoul Guest
-
jpsoul #5
Re: Type Casting Loaded Swf
Just to be clear, the operation to type cast was:
var app:ISomeInterface = SystemManager(swfLoader.content).application as
ISomeInterface;
where as if the swf was created with ASOnly, by extending Sprite instead of
Application you would need to do this:
var app:ISomeInterface = swfLoader.content as ISomeInterface;
jpsoul Guest
-
levancho #6
Re: Type Casting Loaded Swf
you can also just change this
ldr.contentLoaderInfo.addEventListener(Event.INIT, handleLoaded);
to this
ldr.contentLoaderInfo.addEventListener(Event.COMPL ETE, handleLoaded);
and should work as well
levancho Guest
-
jpsoul #7
Re: Type Casting Loaded Swf
Hi Levancho,
Using the Complete event for the contentLoaderInfo prop didn't work for me. Do
you have an example of this working? I could only get the results I wanted from
the swfloader and the app complete event.
jpsoul Guest
-
jpsoul #8
Re: Type Casting Loaded Swf
I'm attaching my code that I used for testing this. I tried loading an MXML
created swf using both the complete event of the Loader.contentLoaderInfo and I
used the Application Complete event of the SWFLoader. I also loaded a AS Only
created clip with the swf loader. This should demonstrate what I've been
talking about in the thread.
The swf that does the loading and type casting:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="start()">
<mx:Script>
<![CDATA[
import interfaces.ISomeInterface;
import mx.managers.SystemManager;
private var ldr:Loader;
private function start():void
{
ldr = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPL ETE, handleLoadComplete);
var ldrContext:LoaderContext = new LoaderContext(false,
ApplicationDomain.currentDomain);
ldr.load(new URLRequest("AppToLoad.swf"));
}
private function handleLoadComplete(e:Event):void
{
trace("handleCreationComplete");
var myApp:AppToLoad = SystemManager(ldr.content).application as AppToLoad;
trace("myApp = "+myApp);
var withInterface:ISomeInterface = SystemManager(ldr.content).application
as ISomeInterface;
trace("withInterface = "+withInterface);
}
private function handleCreationComplete(e:Event):void
{
trace("handleCreationComplete");
var myApp:AppToLoad = SystemManager(swfLDR.content).application as
AppToLoad;
trace("myApp = "+myApp);
var withInterface:ISomeInterface =
SystemManager(swfLDR.content).application as ISomeInterface;
trace("withInterface = "+withInterface);
}
private function handleASOnlyLoaded(e:Event):void
{
trace("handleASOnlyLoaded");
var asOnly:ISomeInterface = ASOnlyLoader.content as ISomeInterface;
trace("asOnly = "+asOnly);
}
]]>
</mx:Script>
<mx:SWFLoader id="swfLDR" source="AppToLoad.swf" autoLoad="true"
creationComplete="handleCreationComplete(event)" />
<mx:SWFLoader id="ASOnlyLoader" source="ASOnlyApp.swf" autoLoad="true"
complete="handleASOnlyLoaded(event)" />
</mx:Application>
The MXML created app to load:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="start()"
implements="interfaces.ISomeInterface">
<mx:Script>
<![CDATA[
private function start():void
{
trace("AppToLoad started");
}
public function doSomething():void
{
trace("doSomething");
}
]]>
</mx:Script>
</mx:Application>
The app to load created in AS Only:
package
{
import flash.display.Sprite;
import interfaces.ISomeInterface;
public class ASOnlyApp extends Sprite implements ISomeInterface
{
public function doSomething():void
{
trace("ASOnly swf doing something");
}
}
}
The Interface (needs to be in the interfaces package):
package interfaces
{
public interface ISomeInterface
{
function doSomething():void;
}
}
jpsoul Guest
-
jpsoul #9
Re: Type Casting Loaded Swf
Damn, this code worked for me at home but now I'm trying it on my work machine
and its not working anymore. The only swf that I can type cast to an interface
is the ASOnly swf.
This is very frustrating. Anyone have any ideas why this is happening?
jpsoul Guest



Reply With Quote

