openfl / starling

Known as the "Cross-Platform Game Engine", Starling is a popular Stage3D framework for OpenFL and Haxe
Other
236 stars 68 forks source link

Starling pool with dynamic object type ? #55

Closed galibimtiaz closed 7 years ago

galibimtiaz commented 7 years ago

Maybe this is not the place I should query for the solution but maybe it will come handy for others too,

`public class StarlingPool { public var items:Array; private var counter:int;

    public function StarlingPool(type:Class, len:int)
    {
        items = new Array();
        counter = len;

        var i:int = len;
        while(--i > -1)
            items[i] = new type();
    }

    public function getSprite():DisplayObject
    {
        if(counter > 0)
            return items[--counter];
        else
            throw new Error("You exhausted the pool!");
    }

    public function returnSprite(s:DisplayObject):void
    {
        items[counter++] = s;
    }

    public function destroy():void
    {
        items = null;
    }
}`

Here is a Starling Pool Class created by Lee Brimelow I was wondering how can I convert it to Haxe,

I tried like -

`class StarlingPool { public var items:Array<Class>; private var counter:Int;

public function new(type:Class<Dynamic>, len:Int)
{
    items = new Array<Class<Dynamic>>();
    counter = len;

    var i:Int = len;
    while (--i > -1)
        items[i] = type;
}

public function getSprite():Class<Dynamic>
{
    if (counter > 0)
        return items[--counter];
    else
        throw new Error("You exhausted the pool!");
        return null;
}

public function returnSprite(s:Dynamic):Void
{
    items[counter++] = s;
}

public function destroy():Void
{
    items = null;
}

}`

But I dose not work ,maybe I am not casting it properly ?, for example -

pool = new StarlingPool(Bullet, 100); var b:Bullet = cast(pool.getSprite()); //or var b:Bullet = cast(pool.getSprite(),Bullet)

galibimtiaz commented 7 years ago

Sorry I should not ask it here, Stupid me.