Adding array of sprites to the stage?

Hi

I have an array made of objects (extended from Sprite)… I would like to add these to the stage but addChild(array_of_sprites) complaints that the object has to be Openfl object, whoich kind of makes sense. In that context how do I add an array of sprites to the stage? Just run a loop and add the child as individual objects? But then the connection between the array and the objects is lost, I mean if I modify the array then I have to rerun the loops in different places to keep checking to see the items in the stage is equal to the objects in array.

Something like this

class Deck extends Sprite{

    public var cards:Array<Card>;

    public function new (_cards:Array<Card>) {
        
        super ();
            
        cards=_cards;

        addChild(cards);

        }

I guess what I am hoping is to find an optimized cpu friendly way of keeping track on stage. For example I manipulate the array and the stuff on the stage is drawn based on the contents of this array. I know I can add a for loop in the on frame enter listerner, but would be nice if I do do that.

thanks

This should work:

class Deck extends Sprite
{
	public var cards:Array<Card>;

	public function new (_cards:Array<Card>) 
	{
		super ();
		cards = _cards;
		// Loop Through Each Card and add it
		for (card in _cards) addChild(card);
	}
}

Edit: Sorry Didn’t see you wanted a no loop solution and I now see your problem.

Explanation of Solution:

The link between the objects and the array isn’t lost since array with objects (aka not values like Float, Int, Bool) are associative so you can simply do something like cards[0].x = 60; and it will update on screen.

Example:

// Filling the Array
var _cards:Array<Card> = [];
var numCards = 52;
var card:Card;

for (i in 0...numCard)
{
    card = new Card();
    addChild(card);
    _cards.push(card );
}

The association between the object on screen and the array are still together cards[0].x = 100; would change the first card on screen in the array.

I think what you want to do here is have direct access to the children array of DisplayObjectContainers. One of the way is to modify the openfl classes directly, but that is not update-friendly and stuff.

This is an old but still functional library that uses TileSheet.drawTiles() for rendering and mimics the display list structure. In case you don’t know, TileSheet.drawTiles() is the most efficient way to render graphics on native targets. In this library the TileGroup class has a children array directly accessible. Use a loop to addChild() for each card, and then modify the children array directly if you want to change the depth order of the cards. All non-primitive data types are passed by reference so if you change the property of any children you don’t have to re-add the child to see the effect.

DisplayObjectContainer exposes most of the functions you’d need.

Here’s an abstract class you can use. Sample usage:

var deck:Deck = new Deck(/* ... */);
var deckArray:DisplayObjectArray = deck;
deckArray.push(new Card());
deckArray.splice(3, 1);
trace(deckArray);

Hi

Thanks to all those who replied. You guys gave me alot of ideas to try.