views:

633

answers:

4

I have an arrayCollection of objects that extend Sprite, and have bitmaps within them.

I want to display these in a list (or some other component that would allow a user to scroll through them, and see their associated data.)

When I do: myList.dataProvider = myArrayCollection

the list just shows a bunch of lines of [Object, Item] instead of the visual sprites.

Here is a simplified version of my Object:

public class myUIC extends UIComponent
    {

     public var mySprite:Sprite = new Sprite;

     [Embed(source="assets/BGimage.png")]
     public var BGimage:Class;

     public var myBitmap:Bitmap;
     public var wordText:TextField = new TextField;

     public function myUIC(myWord:String)
     {
      this.wordText.text = myWord;
      this.myBitmap = new BGimage;
      this.mySprite.addChild(this.myBitmap);
      this.mySprite.addChild(this.wordText);
      this.addChild(this.mySprite);
     }
    }

Tried many different ways to get it to show up in a List, but can't do it.

A: 

Sounds like you may want to try writing a simple item renderer (perhaps based off UIComponent) that adds the associated sprite the display list of the render using addChild().

cliff.meyers
Thanks. More details on how to accomplish that would be very helpful. The problem I am having is that I can't figure out how to get this Sprite object to show up in an mx component. I can't set it as the source for an mx:Image. So what might the code look like to do this?
Martholomew
Try using a simple container such as a Canvas and call its addChild() method passing the Sprite as the argument. It should show up in the upper left corner.
cliff.meyers
Here's what I have, but it doesn't work. Maybe you can offer a suggestion of how to fix it:<mx:List id="myList" dataProvider="{myDP}"> <mx:itemRenderer> <mx:Component> <mx:Canvas creationComplete="addChild(data)"/> </mx:Component> </mx:itemRenderer> </mx:List>
Martholomew
the above code returns an error: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.
Martholomew
and when I do addChild(data as Sprite) it compiles, but then gives an eror at runtime: Type Coercion failed: cannot convert com.myApp::mySpriteClass@22f75ba1 to mx.core.IUIComponent.)
Martholomew
Along with brd6644's answer, these two posts help:http://www.tdotblog.info/?q=node/10andhttp://www.streamhead.com/how-to-use-images-in-actionscript-3-with-flashdevelop-and-some-other-as3-tips/
Martholomew
Can you post the source for your custom sprite class? Or is it too large?
cliff.meyers
I can get it to display in a canvas, but not when I try to put it in a List or a Repeater...public class myUIC extends UIComponent { public var mySprite:Sprite = new Sprite; [Embed(source="assets/BGimage.png")] public var BGimage:Class; public var word:String; public var myBitmap:Bitmap; private var wordText:TextField = new TextField; public function myUIC(myWord:String) { this.wordText.text = myWord; this.myBitmap = new BGimage; this.mySprite.addChild(this.myBitmap); this.mySprite.addChild(this.wordText); this.addChild(this.mySprite); } }
Martholomew
Can you post your code in your main post so it's more readable?
cliff.meyers
oops, didn't realize that was possible. :)
Martholomew
+1  A: 

See this tutorial: Flex Examples - displaying icons in a flex list control

tst
A: 

try rawChildren.addChild for adding the Sprite

A: 

Here, try using an itemRenderer something like this. It ought to work with any generic DisplayObject. It's grabbing the width and height from the assigned data property, so you might need to set variableRowHeight to true in your actual list for it to work as expected.

package
{
    import flash.display.DisplayObject;
    import mx.controls.listClasses.IListItemRenderer;
    import mx.core.UIComponent;
    import mx.events.FlexEvent;

    /*
    Extending UIComponent means we can add Sprites (or any DisplayObject)
    with addChild() directly, instead of going through the rawChildren property.
    Plus, in this case, we don't need the extra overhead of Canvas's layout code.

    IListItemRenderer lets us use it as a List's itemRenderer. UIComponent already
    implements all of IListItemRenderer except for the data property
    */
    public class SpriteRenderer extends UIComponent implements IListItemRenderer
    {
        // Implementing the data property for IListItemRenderer is really easy,
        // you can find example code in the LiveDocs for IDataRenderer
        private var _data:Object;

        [Bindable("dataChange")]
        public function get data():Object
        {
            return _data;
        }

        public function set data(value:Object):void
        {
            if (value !== _data) {

                // We need to make sure to remove any previous data object from the child list
                // since itemRenderers are recycled
                if (_data is DisplayObject && contains(_data as DisplayObject)) {
                    removeChild(_data as DisplayObject);
                }

                _data = value;

                // Now we just make sure that the new data object is something we can add
                // and add it
                if (_data is DisplayObject) {
                    this.width = (_data as DisplayObject).width;
                    this.height = (_data as DisplayObject).height;

                    addChild(_data as DisplayObject);
                }

                dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
            }
        }

        public function SpriteRenderer()
        {
            super();
        }
    }
}
krichard