I am very new to haxe openfl, I used to develop game with flash and starling , I am confused about the conversion from flash to openfl haxe.
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)
Array<Class>and then redefine it asArray<Class<Dynamic>>? That's rather nonsensical to me. - Ron ThompsonClassobject to something that's being casted to an instance of thatClass. Somewhere in your pool class you need to instantiate that object, likely through theTypestdlib. - Ron Thompsonitems[i] = new type(). That's actually instantiating an object of that class. You aren't. - Ron Thompsonnew type()in haxe. Haxe dose not suppose this kind of syntax. - Galib ImtiazTypeclass in std lib, it should have what you need. - Ron Thompson