PDA

View Full Version : MXML + AS = frustrating!


team08
06-25-2007, 12:40 AM
I am a c+-#? programmer and am getting killed by this programming environment. AS in flex has been great, straight forward, no problems. Now that I am trying to integrate it into MXML, I want to shoot myself. I have a standalone app in AS, that is basically a CAD/drawing program. Now I want it to live inside a nice graphical environment with buttons for controls. I could keep using AS, but it seems most people use MXML. So the class is called "test" and I dont know how to make an object in MXML and then draw it. Here is the MXML code:

<?xml version="1.0" encoding="utf-8"?>
<!--usingas/eightCAD.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script><![CDDATA[
import test;

public var x:test = new test();
]]></mx:Script>
<mx:Button id="b1" label="Draw"
click="x.drawBoard();"/>
</mx:Application>

and the AS code starts as:

package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.events.MouseEvent;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.BitmapData;

public class test extends Sprite
{
private var imgLoader:Loader;
private var boardSprite:Sprite = new Sprite();

Thanks for any help!

drkstr
06-25-2007, 12:51 AM
x is a reserved word in the application scrope. Try this:

--== TestProject.mxml ==--
<?xml version="1.0" encoding="utf-8"?>
<!--usingas/eightCAD.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import pkg.test;

public var myTest:test = new test();
]]>
</mx:Script>
<mx:Button id="b1" label="Draw"
click="myTest.drawBoard();"/>
</mx:Application>

--== pkg/test.as ==--
// ActionScript file
package pkg {

import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.events.MouseEvent;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.BitmapData;

import mx.controls.Alert //added for pupup

public class test extends Sprite
{
private var imgLoader:Loader;
private var boardSprite:Sprite = new Sprite();

public function drawBoard():void {
Alert.show("hello world");
}
}

}

best regards,
...aaron

team08
06-25-2007, 01:54 AM
So this helped a bit but now the compiler seems to choke on the fact that I create objects of another type from main class. So I have:

<?xml version="1.0" encoding="utf-8"?>
<!--usingas/eightCAD.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import pkg.test;

public var myTest:pkg.test = new pkg.test();
]]>
</mx:Script>
<mx:Button id="b1" label="Draw"
click="myTest.drawBoard();"/>
</mx:Application>

and the next class is:

package pkg
{
import flash.display.Sprite;
import flash.display.Graphics;
import flash.geom.Point;
import flash.events.MouseEvent;

public class bezierFunc extends Sprite
{
public var bdSprite:Sprite = new Sprite();
public var mainSprite:test;

Thanks again for the help

drkstr
06-25-2007, 07:40 AM
If I understood your question, I think you just need to import your other class. You can create objects of whatever type you want, as long as you import it first. You can import all classes in the pkg/ directory with:

import pkg.*;

public var myTest:pkg.test = new pkg.test(); I have never seen types declared like this in AS3, which makes me thing it's a bad idea. You should probably stick to the "official" method as shown in my example. I could be wrong though.

Best regards,
...aaron

hangalot
06-25-2007, 01:04 PM
public var myTest:pkg.test = new pkg.test();
I have never seen types declared like this in AS3, which makes me thing it's a bad idea. You should probably stick to the "official" method as shown in my example. I could be wrong though.

that is perfectly decent way of doing it and the only way to resolve potential class name conflicts accross multiple namespaces

team08
06-25-2007, 03:57 PM
When I use import pkg.*; it complains about nested classes. I suppose I could just copy and paste into the MXML file but that seems awfully messy, and I would like to keep some organization. I cannot seem to find any good tutorials about this. Thanks again for your help! Should I just send you my code, are any of you contractors?

seanhess
06-26-2007, 02:01 PM
I think maybe people are missing the point... This confused me too. Is the problem that you have created something in AS3 first, you want to use flash, but it isn't showing up?

Does your project compile and not work, or does it give any errors/warning on compile?

Let me know.

drkstr
06-26-2007, 09:18 PM
I think he is getting a compile error about nested classes which means he is putting more then one class in the same file, or did not define his package correctly. I forget which one causes that error, but I'm pretty sure it's the first. It's hard to tell without seeing the code.

regards,
...aaron

team08
06-27-2007, 09:03 PM
I did create this is AS3 first and am now trying to put it into an MXML project. I have two files, one is test.as which holds ONLY the "test" class. Another file, bezierFunc.as holds the "bezierFunc" class. Both start by defining a package. Should I not wrap the each entire class in a package?

seanhess
06-27-2007, 09:23 PM
Yes, your code should be wrapped by a package statement, but it can be the same for both of those classes if they are in the same folder.

Assume your project has the following structure

Root:
myPackage/
myPackage/test.as
myPackage/bezierFunc.as

Both test.as and bezierFunc.as should have

package myPackage {
...
}

around everything