14 Replies Latest reply on Aug 12, 2010 1:46 AM by kirti kishan

    how to import .as file into flex project?

    fadoogen

      Quick question.....How do you simply import a .as package into a flex project?
      i've been reading that you do not use the <mx:Script> tag.....so what's the proper way to communicate between mxml and .as files?

       

      thanks,

       

      dean

        • 1. Re: how to import .as file into flex project?
          meshgraphics Level 2

          If the as file is in the src directory

          you can just refer to it.  Assuming it has a class in it.  Otherwise, you use the import statement to use a package in sub dirs like:

          import com.myUtils.myBlueBox

           

          myBB:myBlueBox = new myBlueBox();

           

           

          Or you could be talking about namespace?

          1 person found this helpful
          • 2. Re: how to import .as file into flex project?
            fadoogen Level 1

            my .as file (named FLARdemo.as) is in my src folder.

            my code for my Flex project is:

             

             

            <?xml version="1.0" encoding="utf-8"?>

            <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

               xmlns:s="library://ns.adobe.com/flex/spark"

               xmlns:mx="library://ns.adobe.com/flex/mx">

             

             

            var demo:FLARdemo = new FLARdemo();

            addChild(demo);

             

            <fx:Declarations>

            <!-- Place non-visual elements (e.g., services, value objects) here -->

            </fx:Declarations>

            </s:WindowedApplication>

            i get an error:
            Description Resource Path Location Type
            Initializer for 'mxmlContentFactory': cannot parse value of type mx.core.IDeferredInstance from text '
            var demo:FLARdemo = new FLARdemo();
            addChild(demo);
            '. FLARtest.mxml /FLARtest/src line 6 Flex Problem
            as you can see, i've only opened Flex a couple times....thanks for your help.
            -dean
            • 3. Re: how to import .as file into flex project?
              David_F57 Level 5

              hi,

               

              If your source is pure AS and not a component then you still need to declare a script block, you then refer to the script file your app will use.

               

              <fx:Script source="FLARdemo.as"/>    <=== assuming flardemo is application code not a component

               

               

              if you have  component that you want to instantiate but dont need to call it from actionscript then you add it to the stage through mxml, custom components usually have their own namespace (this will be declared automatically as ns1 if you dont specifically create your own).

               

               

              <comp:FLARDemo/>

               

              in actionscript this would be

               

               

               

              <fx:Script>

              <![CDATA[

              import comp.FLARDemo;

               

              private var myDemo:FLARDemo = new FLARDemo();

               

               

              protected function application1_creationCompleteHandler(event:FlexEvent):void

              {

                this.addElement(myDemo);
              }
              ]]>
              </fx:Script>
              David.

               

              1 person found this helpful
              • 4. Re: how to import .as file into flex project?
                meshgraphics Level 2

                To use the addChild method, the target needs to be a bonafide DisplayObject.  It would appear the problem is in the as file.

                • 5. Re: how to import .as file into flex project?
                  fadoogen Level 1

                  Thanks guys,

                   

                  David, thanks man....it worked. uh, the only thing is i'm getting an error in my .as file (not sure if this is new or if i had just overlooked this, before)

                   

                  Description Resource Path Location Type

                  1037: Packages cannot be nested. FLARdemo.as /FLARtest/src line 7 Flex Problem

                   

                  this is my FLARdemo.as code:

                   

                  package {

                  import flash.display.BitmapData;

                  import flash.display.Sprite;

                  import flash.events.Event;

                  import flash.media.Camera;

                  import flash.media.Video;

                  import flash.utils.ByteArray;

                   

                  import org.libspark.flartoolkit.core.FLARCode;

                  import org.libspark.flartoolkit.core.param.FLARParam;

                  import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;

                  import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;

                  import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;

                  import org.libspark.flartoolkit.pv3d.FLARBaseNode;

                  import org.libspark.flartoolkit.pv3d.FLARCamera3D;

                  import org.papervision3d.lights.PointLight3D;

                  import org.papervision3d.materials.shadematerials.FlatShadeMaterial;

                  import org.papervision3d.materials.utils.MaterialsList;

                  import org.papervision3d.objects.primitives.Cube;

                  import org.papervision3d.render.BasicRenderEngine;

                  import org.papervision3d.scenes.Scene3D;

                  import org.papervision3d.view.Viewport3D;

                   

                  [SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]

                   

                  public class FLARdemo extends Sprite

                  {

                  [Embed(source="pat1.pat", mimeType="application/octet-stream")]

                  private var pattern:Class;

                   

                  [Embed(source="camera_para.dat", mimeType="application/octet-stream")]

                  private var params:Class;

                   

                  private var fparams:FLARParam;

                  private var mpattern:FLARCode;

                  private var vid:Video;

                  private var cam:Camera;

                  private var bmd:BitmapData;

                  private var raster:FLARRgbRaster_BitmapData;

                  private var detector:FLARSingleMarkerDetector;

                  private var scene:Scene3D;

                  private var camera:FLARCamera3D;

                  private var container:FLARBaseNode;

                  private var vp:Viewport3D;

                  private var bre:BasicRenderEngine;

                  private var trans:FLARTransMatResult;

                   

                  public function FLARdemo()

                  {

                  setupFLAR();

                  setupCamera();

                  setupBitmap();

                  setupPV3D();

                  addEventListener(Event.ENTER_FRAME, loop);

                  }

                   

                  private function setupFLAR():void

                  {

                  fparams = new FLARParam();

                  fparams.loadARParam(new params() as ByteArray);

                  mpattern = new FLARCode(16, 16);

                  mpattern.loadARPatt(new pattern());

                  }

                   

                  private function setupCamera():void

                  {

                  vid = new Video(640, 480);

                  cam = Camera.getCamera();

                  cam.setMode(640, 480, 30);

                  vid.attachCamera(cam);

                  addChild(vid);

                  }

                   

                  private function setupBitmap():void

                  {

                  bmd = new BitmapData(640, 480);

                  bmd.draw(vid);

                  raster = new FLARRgbRaster_BitmapData(bmd);

                  detector = new FLARSingleMarkerDetector(fparams, mpattern, 80);

                  }

                   

                  private function setupPV3D():void

                  {

                  scene = new Scene3D();

                  camera = new FLARCamera3D(fparams);

                  container = new FLARBaseNode();

                  scene.addChild(container);

                   

                  var pl:PointLight3D = new PointLight3D();

                  pl.x = 1000;

                  pl.y = 1000;

                  pl.z = -1000;

                   

                  var ml:MaterialsList = new MaterialsList({all: new FlatShadeMaterial(pl)});

                   

                  var cube1:Cube = new Cube(ml, 30, 30, 30);

                  var cube2:Cube = new Cube(ml, 30, 30, 30);

                  cube2.z = 50;

                  var cube3:Cube = new Cube(ml, 30, 30, 30);

                  cube3.z = 100;

                   

                  container.addChild(cube1);

                  container.addChild(cube2);

                  container.addChild(cube3);

                   

                  bre = new BasicRenderEngine();

                  trans = new FLARTransMatResult();

                   

                  vp = new Viewport3D();

                  addChild(vp);

                  }

                   

                  private function loop(e:Event):void

                  {

                  bmd.draw(vid);

                   

                  try

                  {

                  if(detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5)

                  {

                  detector.getTransformMatrix(trans);

                  container.setTransformMatrix(trans);

                  bre.renderScene(scene, camera, vp);

                  }

                  }

                  catch(e:Error){}

                  }

                  }

                  }

                  • 6. Re: how to import .as file into flex project?
                    meshgraphics Level 2

                    Holy Canolie!  All of actionscript is in there.

                     

                    The nested error is about package{}.  If its in the src dir you don't need it but there is so much stuff in there you probably need to drop all the Flar stuff in its own dir.

                    So it would be package flar {

                    }

                     

                    //// in main

                     

                    import flar.*;

                    • 7. Re: how to import .as file into flex project?
                      fadoogen Level 1

                      the FLARdemo.as is in the src folder of my project folder (FLARtest).....and the FLARdemo.as path is set to my global actionscript folder, where all the FLAR and Papervision stuff is...

                      the error points to line 7 which is:

                       

                      import flash.utils.ByteArray;

                       

                      why would that be?

                      • 8. Re: how to import .as file into flex project?
                        meshgraphics Level 2

                        Is it line 7 from the main file?

                         

                        The import statement should be good.  If you make a test project and put 1 line it - import ...ByteArray;  It should be good.  If not, you have a problem with the libs, flex sdk.

                        • 9. Re: how to import .as file into flex project?
                          fadoogen Level 1

                          the error is on line 7 of the .as file.......it's got the red circle with the "X" right next to "import flash.utils.ByteArray;"

                          that's the only spot marked by an error....

                          i started a new .as project and threw the "import flash.utils.ByteArray;" line in there......no errors.

                          • 10. Re: how to import .as file into flex project?
                            meshgraphics Level 2

                            Not good.

                             

                            The file might be out of sync with the debugger.  Exit FB and come back in.  That should rebuild the project.

                            • 11. Re: how to import .as file into flex project?
                              fadoogen Level 1

                              i quit FB, and relaunched...did a CLEAN, but still got that same error.

                              this FLARdemo.as is based (actually, it's the exact code) on Lee Brimelow's intro to augmented reality tutorial on www.gotoandlearn.com

                              I'm just trying to do a test, by taking that .as and throwing it into a Flex Air app project.

                               

                              I previously was able to make a quick, easy twitter app in Flex.....that worked just fine.

                               

                              not sure why this new project is giving me problems.

                               

                              btw, thanks for all your suggestions so far.

                              • 12. Re: how to import .as file into flex project?
                                fadoogen Level 1

                                meshgraphics,

                                 

                                would you mind walking me through how i'd use your namespace suggestion, for my project.

                                i think you may be on the right track with that...

                                 

                                thanks again,

                                 

                                dean

                                • 13. Re: how to import .as file into flex project?
                                  meshgraphics Level 2

                                  Youre into some pretty advanced territory.  Youre not going to get far by poking around in the dark.  Start with the basics.

                                  • 14. Re: how to import .as file into flex project?
                                    kirti kishan

                                    hi fadoogen,

                                     

                                    I have been workin on the same concept Augmented Reality (AR) and have been implimenting similar example which has FLARToolkit api implemetation in it. I have sucecssfully created AS3 application on AR, but i am facing real problem when i tried to implement  the same thing in Flex i.e using <mx:Application....>...writing the AS3 code in  <Script> block. I am getting runtime null object reference errors at FLARCamera3D. Can you please share the experience and code if you successfully implmented the AR into Flex 3 mx based application ( but not in AS3 or Flash as i have already implemented it successfully).

                                     

                                    Thanks in Advance!!