Adding and removing an inventory window to the game [SOLVED]


(John Andersson) #1

Hi. If the hero is on the screen and you press I, then the following happens

		if (Input.pressed(Key.I))
		{
			if (inventoryUp == false)
			{
				trace("added inventory");
				FP.world.add(inventory);
				inventoryUp = true;
			}
			else
			{
				trace("removed inventory");
				FP.world.remove(inventory);
				inventoryUp = false;
			}
		}

As you can see, it toggles the inventory. This is the code in inventory.as:

package  
{
import net.flashpunk.Entity;
import net.flashpunk.utils.Key;
import net.flashpunk.utils.Input;
import net.flashpunk.FP
import Talent.Armor;

public class Inventory extends Entity
{
	[Embed(source = "Panels/inventory.oel", mimeType = "application/octet-stream")] private static const INVENTORY:Class;
	
	private var add:int = 0;
	
	private var armor:Armor = new Armor(10, 10);
	
	public function Inventory():void
	{

	}
	
	override public function update():void
	{
		if (Hero.inventoryUp && add == 0)
		{
			var level:InventoryParser = InventoryParser(FP.world.add(new InventoryParser(INVENTORY)));

			FP.world.add(armor);
			add++;
		}
		
		if(Hero.inventoryUp && add != 0)
		{
			var level = null;
			FP.world.remove(armor);
		}
	}
	
	
}
}

For some reason, when the player toggles the inventory (as shown before), the whole content of inventory.as doesn’t get removed. So that’s problem number one.

Problem number two lies within inventory.as.

The inventory code doesn’t add “armor”, but only the inventory window. (this is remedied if I make it so that it adds an armor through “FP.world.add(new armor(blablabalblal))” But I can’t even remove it then, so yeah…

I don’t get it why it doesn’t remove “armor” and the inventory window (the inventory window is generated through an .oel file made in OGMO.

Here is the oel, btw.

package  
{
import net.flashpunk.Entity;
import net.flashpunk.utils.Key;
import net.flashpunk.utils.Input;
import net.flashpunk.FP
import Talent.Armor;

public class Inventory extends Entity
{
	[Embed(source = "Panels/inventory.oel", mimeType = "application/octet-stream")] private static const INVENTORY:Class;
	
	private var add:int = 0;
	
	private var armor:Armor = new Armor(10, 10);
	
	public function Inventory():void
	{

	}
	
	override public function update():void
	{
		if (Hero.inventoryUp && add == 0)
		{
			var level:InventoryParser = InventoryParser(FP.world.add(new InventoryParser(INVENTORY)));

			FP.world.add(armor);
			add++;
		}
		
		if(Hero.inventoryUp && add != 0)
		{
			var level = null;
			FP.world.remove(armor);
		}
	}
	
	
}
}

Thanks in advance


(Bora Kasap) #2

About your Number One problem.

It looks you’re adding another entity when your inventory opened here

var level:InventoryParser = InventoryParser(FP.world.add(new InventoryParser(INVENTORY)));

so, change this “var level:InventoryParser” to “private class variable” and do that in update

level= InventoryParser(FP.world.add(new InventoryParser(INVENTORY)));

and override removed() function in your inventory like that & remove other added stuff when inventory removed

override public function removed():void
{
FP.world.remove(level);
}

And some extra information… You don’t need to use update function to add your “inventory parser” i think? Overriding added function of your inventory may help you very much like that

override public function added():void //IN INVENTORY
{
level = InventoryParser(FP.world.add(new InventoryParser(INVENTORY)));
}

then you don’t need to check for parser added or not added before, also makes better performance for your game…


(Bora Kasap) #3

Your second problem here, I think you can use the same way, so now, you can use another class variable for to use “FP.world.add” for your “armor”, in "override public function added():void" tooo. And you can remove it in your "override public function removed():void"


(John Andersson) #4

THANK YOU SO MUCH! It worked perfectly :slight_smile: