8 Replies Latest reply on Oct 7, 2007 6:17 AM by shahinyan

    loop question (newbie question)

    shahinyan Level 1
      Dear friends i need to loop and add images to some container. Thats what i did:


      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="addStar()">
      <mx:Script>
      <![CDATA[
      import mx.controls.Image;

      [Bindable]
      public var star_array:Array= new Array();
      public var star_img:Image = new Image();

      public function addStar():void{
      var i:int;
      for (i = 1; i < 5; i++)
      {
      star_img= new Image();
      star_img.name = "Image "+(i+1);
      star_img.source=" http://www.explorearmenia.net/images/star.swf";
      star_array.push(star_img);
      star_rating_box.addChild(star_array );

      }

      }
      ]]>
      </mx:Script>
      <mx:HBox id="star_rating_box" width="100%">

      </mx:HBox>
      </mx:Application>


      This doesnt work, everytime some errors. What i can do do achieve this?
      Thank you in advance,
      Mika
        • 1. Re: loop question (newbie question)
          levancho Level 3
          for (i = 1; i < 5; i++)
          {
          star_img= new Image();
          star_img.name = "Image "+(i+1);
          star_img.source=" http://www.explorearmenia.net/images/star.swf";
          star_array.push(star_img);
          star_rating_box.addChild(star_array);

          }


          you might want to add array after is full loaded, not for every loop index, ?
          star_rating_box.addChild(star_array); maybe its better to have that outside of for
          • 2. Re: loop question (newbie question)
            shahinyan Level 1
            It is not working, because of star_array is not a DisplayObject...
            • 3. Re: loop question (newbie question)
              chris.huston.t10 Level 3
              You need to add a display object, so change your code to this:

              star_rating_box.addChild(star_img);

              Vygo
              • 4. Re: loop question (newbie question)
                shahinyan Level 1
                Thats what i did, but it shows range is out of boundaries error for loop.
                So it is kind of strange!

                • 5. Re: loop question (newbie question)
                  chris.huston.t10 Level 3
                  That is strange. I tested the code by making the one change and it worked for me. What version of Flex are you using? I tested with both Flex 2.01 and Flex 3 beta 2 and did not get any errors.

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="addStar()">
                  <mx:Script>
                  <![CDATA[
                  import mx.controls.Image;

                  [Bindable]
                  public var star_array:Array= new Array();
                  public var star_img:Image = new Image();

                  public function addStar():void{
                  var i:int;
                  for (i = 1; i < 5; i++)
                  {
                  star_img= new Image();
                  star_img.name = "Image "+(i+1);
                  star_img.source=" http://www.explorearmenia.net/images/star.swf";
                  star_array.push(star_img);
                  star_rating_box.addChild(star_img);
                  }
                  }

                  ]]>
                  </mx:Script>
                  <mx:HBox id="star_rating_box" width="100%">
                  </mx:HBox>
                  </mx:Application>

                  Vygo
                  • 6. Re: loop question (newbie question)
                    shahinyan Level 1
                    probably the problem is somewhere else and now i am struggling with this:
                    I am using above code as a itemRenderer for dataGrid item:
                    <mx:DataGridColumn dataField="rating" headerText="Sort by star rating" id="hotel_rating" width="150" itemRenderer="renderers.star_renderer"/>

                    Then itemRenderer looks like this:
                    <mx:HBox xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="addStar()">
                    <mx:Script>
                    <![CDATA[
                    import mx.controls.Image;

                    [Bindable]
                    public var star_array:Array= new Array();
                    public var star_img:Image = new Image();

                    public function addStar():void{
                    var i:int;
                    for (i = 1; i < 5; i++)
                    {
                    star_img= new Image();
                    star_img.name = "Image "+(i+1);
                    star_img.source=" http://www.explorearmenia.net/images/star.swf";
                    star_array.push(star_img);
                    star_rating_box.addChild(star_img);
                    }
                    }

                    ]]>
                    </mx:Script>
                    <mx:HBox id="star_rating_box" width="100%">
                    </mx:HBox>
                    </mx:HBox>

                    And this gives
                    Error #1009: Cannot access a property or method of a null object reference.
                    at renderers::star_renderer/addStar()

                    And what is strange after i am hitting Dismiss All button it shows what it suppose to show...
                    What may cause this trouble?
                    Thanks in advance!
                    • 7. Re: loop question (newbie question)
                      chris.huston.t10 Level 3
                      Here is my sample app. I am not getting any errors, so I wonder if your problem is somewhere else. It looks like you are trying to access a variable in your itemRenderer from your main app before the renderer has been completed.

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

                      [Bindable]
                      private var ac:ArrayCollection = new ArrayCollection([{hotel:"Hilton", rating:4}, {hotel:"Motel 2", rating:2}, {hotel:"Bates Inn", rating:1}]);

                      ]]>
                      </mx:Script>
                      <mx:DataGrid x="10" y="10" id="list" dataProvider="{ac}">
                      <mx:columns>
                      <mx:DataGridColumn headerText="Hotel" dataField="hotel"/>
                      <mx:DataGridColumn dataField="rating" headerText="Sort by star rating" id="hotel_rating" width="150" itemRenderer="renderers.star_renderer"/>
                      </mx:columns>
                      </mx:DataGrid>
                      </mx:Application>

                      Here is the itemRenderer:

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:HBox xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="addStar()">
                      <mx:Script>
                      <![CDATA[
                      import mx.controls.Image;

                      [Bindable]
                      public var star_array:Array= new Array();
                      public var star_img:Image = new Image();

                      public function addStar():void{
                      for (var i:uint = 1; i <= data.rating; i++) {
                      star_img= new Image();
                      star_img.name = "Image "+(i+1);
                      star_img.source=" http://www.explorearmenia.net/images/star.swf";
                      star_array.push(star_img);
                      star_rating_box.addChild(star_img);
                      }
                      }

                      ]]>
                      </mx:Script>
                      <mx:HBox id="star_rating_box" width="100%">
                      </mx:HBox>
                      </mx:HBox>

                      Vygo
                      • 8. Re: loop question (newbie question)
                        shahinyan Level 1
                        Thanks a lot! I've rebuild project and it works perfect. Although this is what i did to make it work another way:

                        <mx:DataGrid id="myhotelsList_dg" dataProvider="{hotelsList}" width="95%" rowCount="4">
                        <mx:columns>
                        <mx:DataGridColumn dataField="thumbnail" headerText="" id="hotel_image" itemRenderer="renderers.gridImageRenderer" width="126" />
                        <mx:DataGridColumn dataField="title" headerText="Sort by Hotel name" id="hotel_title" width="350" itemRenderer="renderers.titleTeaser"/>
                        <mx:DataGridColumn dataField="rating" headerText="Sort by star rating" id="hotel_rating" width="150" >

                        <mx:itemRenderer>
                        <mx:Component>
                        <render:star_renderer_new rating="{data.rating}" />
                        </mx:Component>
                        </mx:itemRenderer>
                        </mx:DataGridColumn>


                        </mx:columns>
                        </mx:DataGrid>


                        itemRenderer:

                        <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml">

                        <mx:Script>
                        <![CDATA[

                        /**
                        * The rating value to be displayed by this component.
                        */
                        [Bindable]
                        public var rating:Number = 0;

                        ]]>
                        </mx:Script>

                        <mx:HBox >



                        <mx:Repeater id="stars" dataProvider="{[1,2,3,4,5]}" count="5">
                        <mx:Image id="starIcon" width="13" alpha="{(rating > stars.currentIndex) ? 1 : .35}" source=" http://www.explorearmenia.net/images/star.swf" />
                        </mx:Repeater>



                        </mx:HBox>

                        </mx:Canvas>