13 Replies Latest reply on Dec 30, 2009 3:15 PM by rajujagad

    Errors in Code

    Tigomark
      OK so here is my
      component

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="200" height="130" backgroundColor="#000000" color="#ff0000" fontSize="20">
      <mx:Script source="../asa/clock.as" />
      </mx:Canvas>

      My AS file

      package
      {
      import flash.display.Sprite;
      import flash.events.TimerEvent;
      import flash.utils.Timer;

      public class ShortTimer extends Sprite
      {
      public function ShortTimer()
      {
      // creates a new five-minute Timer
      var minuteTimer:Timer = new Timer(1000, 300);

      // designates listeners for the interval and completion events
      minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
      minuteTimer.addEventListener(TimerEvent.TIMER_COMP LETE, onTimerComplete);

      // starts the timer ticking
      minuteTimer.start();
      }

      public function onTick(evt:TimerEvent):void
      {
      // displays the tick count so far
      // The target of this event is the Timer instance itself.
      trace("tick " + evt.target.currentCount);
      }

      public function onTimerComplete(evt:TimerEvent):void
      {
      refresh();
      }
      }
      }

      and my application file

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" width="100%" viewSourceURL="srcview/index.html" height="100%"
      xmlns:c="Com_Board.*">
      <mx:Style source="source.css"/>
      <mx:Image source="images/title.jpg" width="210" height="65" top="4" left="15"/>
      <mx:Image source="images/title1.jpg" id="image1" horizontalAlign="right" verticalAlign="top" width="784" height="58" y="3" right="0"/>
      <mx:XML id="tempXML" source=" http://something.net/interface.php?username=admin&amp;password=pass&amp ;customer=63&amp;action=showopen&amp;operation=def ects&amp;format=xml&amp;critcal=both" />
      <c:Red_co x="627" y="69" width="394" height="32" color="#ff0000" fontSize="25"/>

      <mx:XMLListCollection id="defectXMLList" source="{tempXML.defect}" />

      <mx:TileList
      dataProvider="{tempXML.defect.asset}"
      horizontalCenter="0" bottom="56" top="72" width="100%" backgroundColor="#000000" color="#ff0000" fontSize="27" borderColor="#000000" columnWidth="300" rowHeight="50" themeColor="#808080" fontWeight="bold" allowMultipleSelection="true"/>




      </mx:Application>


      All this and I still get the error

      1037: Packages cannot be nested
        • 1. Re: Errors in Code
          ntsiii Level 3
          You are confusing two separate ways of using code in an AS file.

          The first, using mxScript source="myfile.as", is an "include", which copies the contents of the myfile.as directly into the main app, then compiles it.

          The other way is is to write a Class in an as file, as you show.

          When an mxml file is compiled, it becomes its own class. therefore, when you include a class file using source=, you are putting a class(with a package) inside another class, hence the error.

          If you want to use the include method, remove the package and class lines from the as file.

          If you want to use the as file as a class, then instantiate it first. in your main mxml file:
          <ShortTimer id="st" .../>

          Then in as code in the main app, you can do:
          st.getShortTimer.

          NOTE: using the same name for a function as the class name makes that function a constructor. Don't do it unless you know what you are doing.

          Tracy
          • 2. Re: Errors in Code
            Tigomark Level 1
            Ok I guess I'm just dense. I have added in the main application

            <ShortTimer id="st" source="../Com_Board/Red_co.mxml"/>
            <mx:Script>
            <![CDATA[
            st.getShortTimer
            ]]>
            </mx:Script>

            But now I get

            Could not resolve <ShortTimer> to a component implementation.

            This is my first time back in the Flash/Flex world since MX Studio and I am sooo lost at this point but I thank you for your help.
            • 3. Re: Errors in Code
              ntsiii Level 3
              First, declare the defualt namespace on the application tag:
              xmlns="*" this is for conmponents in the current folder.

              then in the script block:
              import ShortTimer;

              Tracy
              • 4. Re: Errors in Code
                ntsiii Level 3
                And what are you trying to do with this?
                source="../Com_Board/Red_co.mxml

                Tracy
                • 5. Re: Errors in Code
                  Tigomark Level 1
                  I have wrapped the as to the component.
                  • 6. Re: Errors in Code
                    ntsiii Level 3
                    Say again? You do not use the "source" property with an instantiated class.

                    Remove that.

                    Tracy
                    • 7. Re: Errors in Code
                      ntsiii Level 3
                      Are you saying that your ShortTimer ie implemented in the"Red_co.mxml" file?

                      Or what?

                      Tracy
                      • 8. Re: Errors in Code
                        Tigomark Level 1
                        Red_co.mxml is a component file that simply accesses the as file.
                        I then call the component to the main application or at least that is my attempt. Thanks for talking this through.
                        • 9. Re: Errors in Code
                          ntsiii Level 3
                          "...accesses the as file..." What does that mean?

                          "...call the component to the main application ..." and what does that mean?

                          Please be clear on what you are doing. Post some code if necessary.

                          Tracy
                          • 10. Re: Errors in Code
                            Tigomark Level 1
                            Application Red_Board.mxml

                            <?xml version="1.0" encoding="utf-8"?>
                            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" width="100%" viewSourceURL="srcview/index.html" height="100%"
                            >
                            <mx:Style source="source.css"/>
                            <mx:Image source="@Embed('images/title.jpg')" width="210" height="65" top="4" left="15"/>
                            <mx:Image source="@Embed('images/title1.jpg')" id="image1" horizontalAlign="right" verticalAlign="top" right="0" scaleContent="true" y="0"/>
                            <mx:XML id="tempXML" source=" http://something.net/interface.php?username=admin&amp;password=pass&amp;customer=63&amp;ac tion=showopen&amp;operation=defects&amp;format=xml&amp;critcal=both" />

                            <ShortTimer id="st" xmlns="Com_Board/Red_co.mxml" />
                            <mx:Script>
                            <![CDATA[
                            st.getShortTimer.
                            ]]>
                            </mx:Script>
                            <!--<c:Red_co y="66" width="394" height="51" color="#ff0000" fontSize="25" x="10" xmlns:c="Com_Board.*"/>-->
                            <mx:TileList
                            dataProvider="{tempXML.defect.asset}"
                            width="100%" backgroundColor="#000000" color="#ff0000" fontSize="27" borderColor="#000000" columnWidth="300" rowHeight="50" themeColor="#808080" fontWeight="bold" allowMultipleSelection="true" height="496" verticalCenter="9" horizontalCenter="0"/>


                            <mx:XMLListCollection id="defectXMLList" source="{tempXML.defect}" />




                            </mx:Application>


                            Component Red_co.mxml

                            <?xml version="1.0" encoding="utf-8"?>
                            <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="394" height="51" backgroundColor="#000000" color="#ff0000" fontSize="20">
                            <mx:Script source="../asa/ShortTimer.as" />
                            </mx:Canvas>

                            As file ShortTimer.as

                            package asa
                            {
                            import flash.display.Sprite;
                            import flash.events.TimerEvent;
                            import flash.utils.Timer;

                            public class ShortTimer extends Sprite
                            {

                            public function ShortTimer()
                            {
                            // creates a new five-minute Timer
                            var minuteTimer:Timer = new Timer(1000, 300);

                            // designates listeners for the interval and completion events
                            minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
                            minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

                            // starts the timer ticking
                            minuteTimer.start();
                            }

                            public function onTick(evt:TimerEvent):void
                            {
                            // displays the tick count so far
                            // The target of this event is the Timer instance itself.
                            trace("tick " + evt.target.currentCount);
                            }

                            public function onTimerComplete(evt:TimerEvent):void
                            {
                            trace("Time's Up!");
                            var minuteTimer:Timer = evt.target as Timer;
                            minuteTimer.reset();
                            minuteTimer.start();
                            }


                            }
                            }
                            • 11. Re: Errors in Code
                              Tigomark Level 1
                              I've tried to keep the as to be accessed by the component because I may add to it later
                              • 12. Re: Errors in Code
                                Tigomark Level 1
                                Any one have any ideas
                                • 13. Re: Errors in Code
                                  rajujagad

                                  Hello

                                   

                                  I am having same trouble using  CS4 and ActionScript 3. Let me illustrate using ShapleExample.as file that is a package.

                                   

                                  package {
                                      import flash.display.DisplayObject;
                                      import flash.display.Graphics;
                                      import flash.display.JointStyle;
                                      import flash.display.LineScaleMode;
                                      import flash.display.Shape;
                                      import flash.display.Sprite;

                                      public class ShapeExample extends Sprite {
                                          private var size:uint           = 80;
                                          private var bgColor:uint       = 0xFFCC00;
                                          private var borderColor:uint  = 0x666666;
                                          private var borderSize:uint   = 0;
                                          private var cornerRadius:uint = 9;
                                          private var gutter:uint       = 5;


                                          private function refreshLayout():void {
                                              var ln:uint = numChildren;
                                              var child:DisplayObject;
                                              var lastChild:DisplayObject = getChildAt(0);
                                              lastChild.x = gutter;
                                              lastChild.y = gutter;
                                              for (var i:uint = 1; i < ln; i++) {
                                                  child = getChildAt(i);
                                                  child.x = gutter + lastChild.x + lastChild.width;
                                                  child.y = gutter;
                                                  lastChild = child;
                                              }
                                          }

                                          private function doDrawCircle():void {
                                              var child:Shape = new Shape();
                                              var halfSize:uint = Math.round(size/2);
                                              child.graphics.beginFill(bgColor);
                                              child.graphics.lineStyle(borderSize, borderColor);
                                              child.graphics.drawCircle(halfSize, halfSize, halfSize);
                                              child.graphics.endFill();
                                              addChild(child);
                                          }

                                          private function doDrawRoundRect():void {
                                              var child:Shape = new Shape();
                                              child.graphics.beginFill(bgColor);
                                              child.graphics.lineStyle(borderSize, borderColor);
                                              child.graphics.drawRoundRect(0, 0, size, size, cornerRadius);
                                              child.graphics.endFill();
                                              addChild(child);
                                          }

                                          private function doDrawRect():void {
                                              var child:Shape = new Shape();
                                              child.graphics.beginFill(bgColor);
                                              child.graphics.lineStyle(borderSize, borderColor);
                                              child.graphics.drawRect(0, 0, size, size);
                                              child.graphics.endFill();
                                              addChild(child);
                                          }
                                      }
                                  }

                                  ******End Code ShapeExample.as*******

                                  Then I create mycode.fla file

                                  and in frame one layer one I type the following code

                                  import class ShapeEample();

                                            doDrawCircle();
                                              doDrawRoundRect();
                                              doDrawRect();
                                              refreshLayout();


                                  ****END MYCODE.FLA***

                                   

                                  i SHOULD GET ONE CIRCLE AND TWO SQUARES** INSTEAD i GET ERROR CODE 1037