[SOLVED] Creating Multiple Entities with create(); function from a STATIC CLASS?


(Bora Kasap) #1

FP.world.addList(); works great when i push elements to the array by

"FP.world.add(new XXXEntity(params));"

have no problem about that…

but now i’m trying to push elements to the array by

(FP.world.create(XXXEntity) as XXXEntity).init(params);

but this time FP.world.addList(); doesnt work…

also this works great by alone, i mean problem not about the init function, it works great when i create entities one by one;

(FP.world.create(XXXEntity) as XXXEntity).init(params);

Which method i should use to create multiple entities?


(Bora Kasap) #2

Using a for loop with an array of entities(parameters setted up one by one one time) solves my problem?

And then “for i” --> FP.world.add(list[i]); FP.world.remove(list[i]);


(Jacob Albano) #3

Are you doing it like this?

FP.world.addList((FP.world.create(XXXEntity) as XXXEntity).init(params));

If so, the problem is likely that your init() function returns void and thus your created entity is never returned to the addList() function.


(Bora Kasap) #4

it is returning and working great… no problem about that, but this function is in a global static class, maybe this is a problem?


(Jacob Albano) #5

Possibly? I can’t imagine why though. Can you paste the contents of that static function in here?


(Bora Kasap) #6

this is where the entity list creating

public static function settings():void
	{
		clearways.push(new int(0));
		availableways.push(new int(1));
		availableways.push(new int(3));
		availableways.push(new int(7));
		
		var xoff:Number = 24;
		var yoff:Number = 35;
		
		leftuilist.push(Typing(FP.world.create(Typing)).init(1 + xoff, 25 + yoff, "LEVEL:", 0xFFFFFF, -1, 0, 1, false, true));
		leveltext = new DynamicTyping(53 + xoff, 25 + yoff, level.toString(), 0xFFFF00, 1, true);
		leftuilist.push(leveltext);
		leftuilist.push(Typing(FP.world.create(Typing)).init(100 + xoff, 25 + yoff, "XP:", 0xFFFFFF, -1, 0, 1, false, true));
		xptext = new DynamicTyping(130 + xoff, 25 + yoff, xp.toString() + "/" + xpnext.toString(), 0xFFFF00, 1, true);
		leftuilist.push(xptext);
		
		xoff = 25;
		yoff = 67;
		
		var col1:Number = 0 + xoff;
		var col2:Number = 178 + xoff;
		var col3:Number = 240 + xoff;
		var col4:Number = 300 + xoff;
		var col5:Number = 360 + xoff;
		var col6:Number = 420 + xoff;
		
		uilist.length = 0;
		customizeui = new CustomizeUI();
		uilist.push(customizeui);
		uilist.push(Typing(FP.world.create(Typing)).init(col3, 58 + yoff, "ADD", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col4, 58 + yoff, "REMOVE", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 42 + yoff, "ADD", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 58 + yoff, "ALL", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 42 + yoff, "REMOVE", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 58 + yoff, "ALL", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 42 + yoff, "CUSTOMIZE", 0xFFFF00, -1, 0, 1, false, true));
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 58 + yoff, "Points Left:", 0xFFFFFF, -1, 0, 1, false, true));
		leftstatstext = new DynamicTyping(103 + xoff, 58 + yoff, leftstats.toString(), 0xFFFF00, 1, true) as DynamicTyping
		uilist.push(leftstatstext);
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 90 + yoff, "AMMO CAPACITY", 0xFFFFFF, -1, 0, 1, false, true));
		maxammotext = new DynamicTyping(col2, 90 + yoff, maxammo.toString(), 0xFFFF00) as DynamicTyping;
		uilist.push(maxammotext);
		uilist.push(Typing(FP.world.create(Typing)).init(col3, 90 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col4, 90 + yoff, "(-)", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 90 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 90 + yoff, "(-)", 0xFF0000));
		uilist.push(new Marker("maxammo", col3, 90 + yoff, "button", "add"));
		uilist.push(new Marker("maxammo", col4, 90 + yoff, "button", "remove"));
		uilist.push(new Marker("maxammo", col5, 90 + yoff, "button", "addall"));
		uilist.push(new Marker("maxammo", col6, 90 + yoff, "button", "removeall"));
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 106 + yoff, "MAX ARMOR", 0xFFFFFF, -1, 0, 1, false, true));
		maxarmortext = new DynamicTyping(col2, 106 + yoff, maxarmor.toString(), 0xFFFF00) as DynamicTyping;
		uilist.push(maxarmortext);
		uilist.push(Typing(FP.world.create(Typing)).init(col3, 106 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col4, 106 + yoff, "(-)", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 106 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 106 + yoff, "(-)", 0xFF0000));
		uilist.push(new Marker("maxarmor", col3, 106 + yoff, "button", "add"));
		uilist.push(new Marker("maxarmor", col4, 106 + yoff, "button", "remove"));
		uilist.push(new Marker("maxarmor", col5, 106 + yoff, "button", "addall"));
		uilist.push(new Marker("maxarmor", col6, 106 + yoff, "button", "removeall"));
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 122 + yoff, "DAMAGE", 0xFFFFFF, -1, 0, 1, false, true));
		damagetext = new DynamicTyping(col2, 122 + yoff, bulletdamage.toString(), 0xFFFF00) as DynamicTyping;
		uilist.push(damagetext);
		uilist.push(Typing(FP.world.create(Typing)).init(col3, 122 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col4, 122 + yoff, "(-)", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 122 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 122 + yoff, "(-)", 0xFF0000));
		uilist.push(new Marker("bulletdamage", col3, 122 + yoff, "button", "add"));
		uilist.push(new Marker("bulletdamage", col4, 122 + yoff, "button", "remove"));
		uilist.push(new Marker("bulletdamage", col5, 122 + yoff, "button", "addall"));
		uilist.push(new Marker("bulletdamage", col6, 122 + yoff, "button", "removeall"));
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 138 + yoff, "ATTACK SPEED", 0xFFFFFF, -1, 0, 1, false, true));
		attackstattext = new DynamicTyping(col2, 138 + yoff, "+" + attackstat.toString() + "%", 0xFFFF00) as DynamicTyping;
		uilist.push(attackstattext);
		uilist.push(Typing(FP.world.create(Typing)).init(col3, 138 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col4, 138 + yoff, "(-)", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 138 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 138 + yoff, "(-)", 0xFF0000));
		uilist.push(new Marker("attackstat", col3, 138 + yoff, "button", "add"));
		uilist.push(new Marker("attackstat", col4, 138 + yoff, "button", "remove"));
		uilist.push(new Marker("attackstat", col5, 138 + yoff, "button", "addall"));
		uilist.push(new Marker("attackstat", col6, 138 + yoff, "button", "removeall"));
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 154 + yoff, "ATTACK RANGE", 0xFFFFFF, -1, 0, 1, false, true));
		attackrangetext = new DynamicTyping(col2, 154 + yoff, "+" + attackrange.toString() + "%", 0xFFFF00) as DynamicTyping;
		uilist.push(attackrangetext);
		uilist.push(Typing(FP.world.create(Typing)).init(col3, 154 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col4, 154 + yoff, "(-)", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 154 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 154 + yoff, "(-)", 0xFF0000));
		uilist.push(new Marker("attackrange", col3, 154 + yoff, "button", "add"));
		uilist.push(new Marker("attackrange", col4, 154 + yoff, "button", "remove"));
		uilist.push(new Marker("attackrange", col5, 154 + yoff, "button", "addall"));
		uilist.push(new Marker("attackrange", col6, 154 + yoff, "button", "removeall"));
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 170 + yoff, "SPEED", 0xFFFFFF, -1, 0, 1, false, true));
		motorpowertext = new DynamicTyping(col2, 170 + yoff, "+" + motorpower.toString() + "%", 0xFFFF00) as DynamicTyping;
		uilist.push(motorpowertext);
		uilist.push(Typing(FP.world.create(Typing)).init(col3, 170 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col4, 170 + yoff, "(-)", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 170 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 170 + yoff, "(-)", 0xFF0000));
		uilist.push(new Marker("motorpower", col3, 170 + yoff, "button", "add"));
		uilist.push(new Marker("motorpower", col4, 170 + yoff, "button", "remove"));
		uilist.push(new Marker("motorpower", col5, 170 + yoff, "button", "addall"));
		uilist.push(new Marker("motorpower", col6, 170 + yoff, "button", "removeall"));
		uilist.push(Typing(FP.world.create(Typing)).init(col1, 186 + yoff, "TARGETLOCK", 0xFFFFFF, -1, 0, 1, false, true));
		magnetpowertext = new DynamicTyping(col2, 186 + yoff, magnetpower.toString() + "%", 0xFFFF00) as DynamicTyping;
		uilist.push(magnetpowertext);
		uilist.push(Typing(FP.world.create(Typing)).init(col3, 186 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col4, 186 + yoff, "(-)", 0xFF0000));
		uilist.push(Typing(FP.world.create(Typing)).init(col5, 186 + yoff, "(+)", 0x00FF00));
		uilist.push(Typing(FP.world.create(Typing)).init(col6, 186 + yoff, "(-)", 0xFF0000));
		uilist.push(new Marker("magnetpower", col3, 186 + yoff, "button", "add"));
		uilist.push(new Marker("magnetpower", col4, 186 + yoff, "button", "remove"));
		uilist.push(new Marker("magnetpower", col5, 186 + yoff, "button", "addall"));
		uilist.push(new Marker("magnetpower", col6, 186 + yoff, "button", "removeall"));
		
		settingsloaded = true;
	}

And this one to adding entities to world inside same class;

public static function customizePanel():void
	{
		if (!uiopen)
		{
			FP.world.addList(uilist);
			FP.world.addList(leftuilist);
			uiopen = true;
			leftuiopen = true;
			newmessage = false;
			ingame = false;
			STORE.menu.play();
			cursor.text = leftstats.toString();
			STORE.music.volume = 0.25;
		}
		else
		{
			FP.world.removeList(uilist);
			FP.world.removeList(leftuilist);
			uiopen = false;
			leftuiopen = false;
			ingame = true;
			STORE.menu.play();
			cursor.text = null;
			STORE.music.volume = 1;
		}
	}

when i use all this code, all other entities adding to world but not any of “Typing”:


(Bora Kasap) #7

Ahhh, i think i got it! I’m calling “settings()” function from the world which initializing with game start, but i’m calling “customizePanel()” from another world. Maybe flashpunk converting “FP.world” to “StartScreenWorld” because of FP.world called while the world is StartScreenWorld and adding entities to the list with parameter like that “StartScreenWorld.create(…)”… I’m gonna try to call settings() from the world i want to call customizePanel() function. What you think about that? I don’t wanna make it again and again, so i’m waiting for your suggestion…


(Bora Kasap) #8

Yeah i think thats the problem. FP.world returning current world of course. So i need to add “FP.world” code to list without it is returning current world. But of course it is gonna return an error message. So now, i’m thinking about it… And now i found an idea, i can’t wait more, i’m gonna add settings function into the “FP.world” class(with making addToWorld:Boolean = false) and run it once.


(Bora Kasap) #9

It doesn’t worked :(((( Hellllpppppp :asdasdasdsa


(Bora Kasap) #10

SOLVED:

got the problem, it is about create() function.

this is the create() function:

public function create(classType:Class, addToWorld:Boolean = true):Entity
	{
		var e:Entity = _recycled[classType];
		if (e)
		{
			_recycled[classType] = e._recycleNext;
			e._recycleNext = null;
		}
		else e = new classType;
		if (addToWorld) return add(e);
		return e;
	}

so, if addToWorld boolean is true, it is returning add(e), then check it out what is “add()” function? this is the add(); function

public function add(e:Entity):Entity
	{
		_add[_add.length] = e;
		return e;
	}

then, it is solved. if addToWorld is true, it returning e:Entity; but if addToWorld is false, it is returning: e:classType,

so it is worked when i make “addToWorld = false”,

Thanks for your help.