PDA

View Full Version : How to include ActionScript in MXML


hugene
11-13-2008, 10:56 PM
Hello, I've been playing all day with MXML, following the online doc. But now I am trying to do something more complicated:

I want to build a window where I can look at the input from my webcam.

So I have the following MXML

<?xml version="1.0"?>
<!-- mxml/TriggerCodeExample2.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script source="includes/CameraExample.as" />

<mx:Script><![CDATA[

public var cameraExample:CameraExample = new CameraExample;

]]></mx:Script>

<mx:Panel title="My Application"
paddingTop="10"
paddingBottom="10"
paddingLeft="10"
paddingRight="10"
>

<mx:Button label="Start Capture" click="cameraExample.CameraExample();"/>

</mx:Panel>
</mx:Application>


and the CameraExample.as is copied from here http://livedocs.adobe.com/flex/3/langref/flash/media/Camera.html#includeExamplesSummary

But I get the following compile error:

: col: 17 Error: Packages can
not be nested.

package {



Why is this happening?

I tried also creating a sub-directory called myCamera and put the CameraExample.as file there instead, and added myCamera after the package keyword. I then put xmlns:cmp="myCamera.*" in the mx:Application tag.

This doesn't work either, I get the following error:

: Error: Call to a possibly undef
ined method CameraExample through a reference with static type CameraExample.

<mx:Button label="Start Capture" click="cameraExample.CameraExample();"/>


I am lost here, seems like the code from CameraExample.as is not properly included in the MXML file. Can someone please advise me on the proper way to include and run CameraExample.as.

Thanks a lot

hugene
11-14-2008, 04:00 AM
Could this be the solution here : http://livedocs.adobe.com/flex/3/html/usingas_5.html#150297 ?

What I don't understand is how do I "produce" the packadge in


import MyPackage.Util.MyClass;


is it a folder "MyPackage", a sub-folder "Util" and a file "MyClass.as" in the sub-folder "Util"?

Thank you

creynders
11-14-2008, 01:01 PM
Normally if you place the CameraExample.as in the same directory as your application mxml file then you can just use it as you did.
If however you defined a package in CameraExample.as then that .as file needs to be in a directory structure analoguous to the package you defined.

creynders
11-14-2008, 01:01 PM
Could this be the solution here : http://livedocs.adobe.com/flex/3/html/usingas_5.html#150297 ?

What I don't understand is how do I "produce" the packadge in


import MyPackage.Util.MyClass;


is it a folder "MyPackage", a sub-folder "Util" and a file "MyClass.as" in the sub-folder "Util"?

Thank you

Yep, that's correct.

wobagi
11-14-2008, 03:33 PM
Hi



<mx:Script source="includes/CameraExample.as" />


You have to understand the difference between including and importing. Include means only copying the contents of a file. The script above includes the CameraExample.as contents. However you cannot define a class inside a <mx:Script> so you get an error. What you have to do is to import the class file. Importing is smarter than including. It imports the definition of a class so you can use it in your code.
You just have to use import statement inside the <mx:Script> block.
Three paths must match: the name of the class in the import statement, the location of the class file and the package name inside the class file.
So if you put the CameraExample.as in the myCamera subfolder you have to:

<mx:Script>
<![CDATA[
import myCamera.CameraExample; // or myCamera.*;
...

and rename the package name inside the CameraExample.as file to:

package myCamera {
//the class definition goes here
...
}


I tried also creating a sub-directory called myCamera and put the CameraExample.as file there instead, and added myCamera after the package keyword. I then put xmlns:cmp="myCamera.*" in the mx:Application tag.

If you don't use the component as a tag, but only in actionscript, you don't have to define another namespace. Just import the class.

Hope this helps.

hugene
11-15-2008, 10:07 PM
Thanks a lot guys, the code now gets imported fine.