I have imported a swf (not created with Flex, i.e. non-framework) into a Flex application. Once loaded, I would like to access movieclips within that imported swf. Looking at Adobe's docs (http://livedocs.adobe.com/flex/3/html/help.html?content=controls_15.html), it seems straightforward; however, their examples are between a Flex app and an imported swf (created with Flex).
Like their example, I'm trying to use the SystemManager to access the imported swf's content; however, I receive the following error:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip#58ca241 to mx.managers.SystemManager.
Is this error occurring because I'm importing a non-framework swf into a framework swf? Thanks in advance for any assistance.
Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:SWFLoader source="assets/test.swf" id="loader" creationComplete="swfLoaded()" />
<mx:Script>
<![CDATA[
import mx.managers.SystemManager;
[Bindable]
public var loadedSM:SystemManager;
private function swfLoaded():void
{
loadedSM = SystemManager(loader.content);
}
]]>
</mx:Script>
</mx:Application>
Was test.swf created with an earlier AS version? According to this swfs published for AS 1.0/2.0 runs in a different AS virtual machine than AS 3.
The parent AVM2 SWF file will not have
access to the properties, methods, or
objects of the loaded AVM1Movie
object.
You can access them directly, using their instance names.
private function swfLoaded():void {
var clip1:MovieClip = MovieClip(loader.content.myClip1);
var clip2:MovieClip = MovieClip(loader.content.myClip2);
// ...
}
Related
I have a very simple Flex 4 app, the mxml of which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
minWidth="400" minHeight="300">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
public function Execute(foo:Object):void {
Alert.show("Hello World");
}
]]>
</fx:Script>
</s:Application>
I am compiling this using flex/bin/mxmlc file.mxml.
In another Flex app, I am attempting to load the app above using an SWFLoader, and to run the Execute function. It's code looks similar to:
var swfLoader:SWFLoader = ...;
// on load, run the below
var loadedSM:SystemManager = SystemManager(swfLoader.content);
var loadedApp:Object = loadedSM.application;
loadedApp.Execute(this);
The outer app is loaded just fine by the browser, but when the SWFLoader loads the inner app, the outer app crashes / unloads: the browser window goes white.
In the debug log from Flash Player, this is what I see:
VerifyError: Error #1053: Illegal override of getElementNearestScrollPosition in spark.layouts.HorizontalLayout.
ReferenceError: Error #1065: Variable ImageSkin__embed_mxml_Assets_swf___brokenImage_818059060 is not defined.
ReferenceError: Error #1065: Variable _0c91adc980e321d1cb58dff2b8f06798859d4954576f5b2afe24ac0f6c486f60_flash_display_Sprite is not defined.
Any pointers at all?
Within an application, you use the spark:ModuleLoader to load spark modules or applications (spark:Module or spark:Application). This takes care of the dependencies, as well as, the loading process.
I'm trying to make VideoDisplay playing media with FlashDevelop. Here's the source of my application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.events.VideoEvent;
private function pause():void
{
if (moo_player.state == VideoEvent.PLAYING)
moo_player.pause(); else
if (moo_player.state == VideoEvent.PAUSED)
moo_player.play();
}
]]>
</mx:Script>
<mx:Panel>
<mx:VideoDisplay
source="bar.flv"
width="640"
height="480"
maintainAspectRatio="true"
id="moo_player"
autoPlay="true"
doubleClick="pause();"
doubleClickEnabled="true"
/>
</mx:Panel>
</mx:Application>
The problem is when i build application and run it (unfortunately, got no idea how to run it without KMPlayer or Mozilla - Flash Player is a plugin afaik) i got no video. The movie file is in the same directory as application's "Application.flv" one. But if i reload application (within player or browser) a few times, video starts.
So, here are my questions:
what's wrong with VideoDisplay
component and how to fix this
'non-playing'?
what's the better way
to execute application than running
it within movie player or browser?
P.S.: please, do not get mad of my knowledge lacks - i began to use Flex nearly 30 minutes ago.
You should be using Spark components, not MX components. Try this:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:VideoPlayer source="bar.flv" width="640" height="480" />
</s:Application>
There's some issues with video display internally in the component. One of the only flex components that's kind of poorly done in some ways. Please don't let it discourage you from exploring Flex.
Create a custom component that extends it, create a file named CustomVideoDisplay.as with this code:
package
{
import mx.controls.VideoDisplay;
public class CustomVideoDisplay extends VideoDisplay
{
[Bindable]
override public function get source():String
{
return super.source;
}
override public function set source(value:String):void
{
super.source = value;
play();
}
public function CustomVideoDisplay()
{
super();
}
}
}
Then add this into your root <application> tag :
xmlns:local="*"
And for your video component, refer to it as:
<local:CustomVideoDisplay
source="bar.flv"
width="640"
height="480"
maintainAspectRatio="true"
id="moo_player"
autoPlay="true"
doubleClick="pause();"
doubleClickEnabled="true"
/>
Let me know if this doesn't do the trick for you!
Well, i thought: my player will be ran at client-side of web project, and in FireFox that code runs successfully each of seven runs. I think this would be enough for testing and implementation.
Thanks everyone for the trouble-taking!
this is a follow up question from this one, I don't want to keep going in the comments and preventing people from getting hard-earned reputation... :)
In my Cairngorm command class, to get it to compile I needed to tell it what myCanvas was, so I used this line:
var myCanvas : MyCanvas = new MyCanvas;
I'm guessing that's wrong, though, because although it compiles, if I try to do something like this:
if (myCanvas.subObject.value == 0) { ... }
it'll throw this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.foo.bar.command::MyCommand/execute()
as if the subObject doesn't exist. It looks like I might be getting a new instance of MyCanvas, not the instance I want from the main.mxml with an id of myCanvas. Am I right? How do I fix this?
Edit (10:59pm GMT+1): Okay, so it looks like I've been way too vague here. Here's my main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:control="com.foo.bar.control.*"
xmlns:business="com.foo.bar.business.*"
xmlns:view="com.foo.bar.view.*"
applicationComplete="com.foo.bar.util.StartupUtil.init()"
horizontalScrollPolicy="off"
verticalScrollPolicy="off"
borderThickness="0"
paddingBottom="0"
paddingLeft="0"
paddingTop="0"
paddingRight="0"
>
<mx:Script>
<![CDATA[
import com.foo.bar.model.PlayerModelLocator;
[Bindable]
private var model : PlayerModelLocator = PlayerModelLocator.getInstance();
]]>
</mx:Script>
<!-- ========================================================================== -->
<!-- the ServiceLocator where we specify the remote services -->
<business:Services id="services" />
<!-- the FrontController, containing Commands specific to this application -->
<control:PlayerController id="controller" />
<!-- ========================================================================== -->
<mx:Style source="assets/main.css" />
<view:MyCanvas id="myCanvas" />
</mx:Application>
And here's my com/foo/bar/command/MyCommand.as:
package com.foo.bar.command {
/* add to controller
addCommand( MyEvent.EVENT_CHANGE_VOLUME, ChangeVolumeCommand );
*/
import flash.net.SharedObject;
import com.adobe.cairngorm.control.CairngormEvent;
import com.adobe.cairngorm.commands.ICommand;
import com.foo.bar.model.PlayerModelLocator;
import com.foo.bar.event.MyEvent;
import com.foo.bar.view.*;
public class ChangeVolumeCommand implements ICommand {
public function execute(event:CairngormEvent):void {
var model : PlayerModelLocator = PlayerModelLocator.getInstance();
var myEvent : MyEvent = MyEvent(event);
var myCanvas : MyCanvas = new MyCanvas();
var so:SharedObject = SharedObject.getLocal("fixie.video");
if (myCanvas.subObject.value == 0) {
trace("subobject value is 0");
}
}
}
}
Basically, I want to get a handle on the object with ID myCanvas in main.mxml using the myCanvas object in MyCommand.as
Could be a couple of things. First, you need parentheses on your class name after the "new" statement: new MyCanvas(); Second, you may be trying to access your sub component before the component lifecycle is ready for you to do so. (It's hard to tell from the code you posted since there isn't enough context.)
What is the scope of your myCanvas variable? Is it inside a method somewhere? You will need to make it public or give it getter/setter to be able to access it.
You may also be trying to reference it before it has been added to its parent, using the addChild() method.
There really isn't enough code in your examples to determine the problem, but these things should give you somewhere to start looking.
1 way is to add eventListener when your myCanvas will be ready after CreationComplete and to do all your stuff
and the second is:
define your subObject as in myCanvas class so you'll be able to access it on Init Stage of your component.
regards
Eugene
p.s. all of the time everybody want to get answer without well formed sample of their problem, its terrible!!
I'm trying to communicate between a flex 4.1 application to a flash action script 2 application using LocalConnection.
flash application
contains a button called btn01 and the following code:
var a:LocalConnection = new LocalConnection();
btn01.onPress = function() {
trace("button clicked");
a.send("abcde","test");
}
you can see here that it sends a test command to the connection named 'abcde'.
flex application
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" initialize="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private function init():void {
var a:LocalConnection = new LocalConnection();
a.client=this;
a.connect("abcde");
}
public function test():void {
Alert.show("test");
}
]]>
</fx:Script>
<mx:SWFLoader source="/location/as2-flash-file.swf" />
as you can see, in the flex application i connect to LocalConnection named 'abcde' and i set the client to 'this' which means that all the public functions can be executed from the LocalConnection.
the SWFLoader element loads the as2 flash file.
whenever i click the button i do see the trace message but the function test does not get executed on the flex application. any ideas?
update
both applications sit on the same domain, on the localhost actually so no need for allowDomain usage and both applications are web based.
Documentation says AS2 and AS3 LocalConnections should communicate no problems.
Do you need to look into the allowDomain method? Do you need to put a crossdomain.xml file in place? If you do have swfs on two different domains, pay special attention to the send method documentation, because you you have to add additional info to the send method's connection name.
Are they both browser based applications? I not, look into AIR
I created the LocalConnection variable within the init() scope, so when the function ended the localconnection was destroyed. the solution is just to declare the variable outside of the init function.
public var a:LocalConnection;
private function init():void {
a = new LocalConnection();
a.client=this;
a.connect("abcde");
}
I have a flex project with a mx:Text.
i have a class that is loaded at the beginning of my project and i want this class to enter text in that text element. the id of the text element is "messagePanel" but when i try to type messagePanel.text i get 'Access of undefined property'. how do i resolve the issue?
example
general.FMS3Connect class connects to an adobe flash media server, when it completes connecting i want it to display the even info code of the connection inside a mx:Box, it's id is messageBox.
on my main mxml file i have the following:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientColors="[0xFFFFFF,0xAAAAAA]"
xmlns:local="*">
<mx:Script>
<![CDATA[
import general.FMS3Connect;
private var conn:FMS3Connect= new FMS3Connect();
]]>
</mx:Script>
<mx:Text id="messageBox" color="black" text="trying to connect to server..." creationComplete="conn.connect()" >
</mx:Application>
the function connect() for now just has "messageBox.text='test'";
when i execute the application i get the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
how do i resolve the issue?
thanks!
Problem is, your FMS3Connect class has to reference to the Text element.
Easiest (but nasty) solution is to pass in a reference to the Text Element to your connect method, you can then reference the element from that.
something like...
<mx:Text id="messageBox" color="black" text="trying to connect to server..." creationComplete="conn.connect( messageBox )" >
public function connect( messageDisplay : Text ) : void {
// do usual connect stuff.
messageDisplay.text = "test";
}
This isn't the nicest solution in the world, connect shouldn't know about the message box really. But it solved your problem!
wait for creationcomplete event