Placing objects from XML doesn't work properly!? [SOLVED]


(John Andersson) #1

Hi. I’m using OGMO files to load a window where the talents/inventory are.

I am using this code in the hero class:

		//Inventory
		if (Input.pressed(Key.I))
		{
			if (inventoryUp == false)
			{
				FP.world.add(inventory);
				inventoryUp = true;
			}
			else
			{
				FP.world.remove(inventory);
				inventoryUp = false;
			}
		}

to add/remove - show/hide the talents/inventory window

this is the inventory/talents class

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

public class Talent_Tree extends Entity
{
	[Embed(source="../Panels/talents.oel", mimeType="application/octet-stream")]private static const TALENTS:Class;
	
	private var add:int = 0;
	private var parser:Talent_TreeParser = new Talent_TreeParser(TALENTS);
	
	public function Talent_Tree():void
	{
		layer = 0;
	}
	
	override public function added():void //IN INVENTORY
	{
		FP.world.add(parser);
	}
	
	override public function removed():void
	{
		FP.world.remove(parser);
	}
	
	override public function update():void
	{
		layer = -1;
		parser.x = FP.world.camera.x;
	}
	
}
}

And this is the treeparser:

package Talents
{
import flash.utils.ByteArray;
import net.flashpunk.Entity;
import net.flashpunk.graphics.Tilemap;
import net.flashpunk.masks.Grid;
import flash.geom.Point;
import net.flashpunk.FP;

public class Talent_TreeParser extends Entity
{
	private var _tiles:Tilemap;
	private var _ground:Grid;
	private var _wall:Grid;
	public var LevelData:XML;
	
	public function Talent_TreeParser(xml:Class)
	{
		_tiles = new Tilemap(Assets.INVENTORY_TILESET, 2160, 1200, 120, 120);
		graphic = _tiles;
		
		type = "talents";
		
		loadInventory(xml);
	}

	private function loadInventory(xml:Class):void 
	{ 
		var rawData:ByteArray = new xml; 
		var dataString:String = rawData.readUTFBytes(rawData.length); 
		LevelData = new XML(dataString);

		var dataElement:XML;
	
		var dataListTiles:XMLList;
		var dLAS:XMLList;
		var dLAE:XMLList;
		var dLAM:XMLList;
		var dLLS:XMLList;
		var dLDV:XMLList;
		var dLMO:XMLList;
		var dLAD:XMLList;
		var dLAP:XMLList;

	
		dataListTiles = LevelData.OurTiles.tile;

		dLAS = LevelData.Upgrades.Armor_Survival
		dLAE = LevelData.Upgrades.Armor_Endurance
		dLAM = LevelData.Upgrades.Armor_Malefic
		dLLS = LevelData.Upgrades.Luck_Stroke
		dLDV = LevelData.Upgrades.Damage_Violent
		dLMO = LevelData.Upgrades.Magic_Occultism
		dLAD = LevelData.Upgrades.Agility_Dagger
		dLAP = LevelData.Upgrades.Agility_Perception
		
		for each(dataElement in dataListTiles)
		{
			_tiles.setTile(int(dataElement.@x), int(dataElement.@y), int(dataElement.@id));
		}
		
		//Upgrade buttons
		
		for each(dataElement in dLAS)
		{
			FP.world.add(new Armor_Survival(new Point(int(dataElement.@x), int(dataElement.@y))));
		}
		
		for each(dataElement in dLAE)
		{
			FP.world.add(new Armor_Endurance(new Point(int(dataElement.@x), int(dataElement.@y))));
		}
		
		for each(dataElement in dLAM)
		{
			FP.world.add(new Armor_Malefic(new Point(int(dataElement.@x), int(dataElement.@y))));
		}
		
		for each(dataElement in dLLS)
		{
			FP.world.add(new Luck_Stroke(new Point(int(dataElement.@x), int(dataElement.@y))));
		}
		
		for each(dataElement in dLDV)
		{
			FP.world.add(new Damage_Violent(new Point(int(dataElement.@x), int(dataElement.@y))));
		}
		
		for each(dataElement in dLMO)
		{
			FP.world.add(new Magic_Occultism(new Point(int(dataElement.@x), int(dataElement.@y))));
		}
		
		for each(dataElement in dLAD)
		{
			FP.world.add(new Agility_Dagger(new Point(int(dataElement.@x), int(dataElement.@y))));
		}
		
		for each(dataElement in dLAP)
		{
			FP.world.add(new Agility_Perception(new Point(int(dataElement.@x), int(dataElement.@y))));
		}
		
	}
}
}

And finally, the XML file.

  <OurTiles tileset="Tile" exportMode="XML">
    <tile x="0" y="0" id="73" />
    <tile x="0" y="1" id="18" />
    <tile x="0" y="2" id="36" />
    <tile x="0" y="3" id="54" />
    <tile x="0" y="4" id="38" />
    <tile x="0" y="5" id="56" />
    <tile x="0" y="6" id="126" />
    <tile x="0" y="7" id="144" />
    <tile x="0" y="8" id="93" />
    <tile x="1" y="0" id="47" />
    <tile x="1" y="1" id="17" />
    <tile x="1" y="2" id="17" />
    <tile x="1" y="3" id="17" />
    <tile x="1" y="4" id="143" />
    <tile x="1" y="5" id="0" />
    <tile x="1" y="6" id="162" />
    <tile x="1" y="7" id="168" />
    <tile x="1" y="8" id="117" />
    <tile x="2" y="0" id="45" />
    <tile x="2" y="1" id="17" />
    <tile x="2" y="2" id="163" />
    <tile x="2" y="3" id="17" />
    <tile x="2" y="4" id="0" />
    <tile x="2" y="5" id="0" />
    <tile x="2" y="6" id="44" />
    <tile x="2" y="7" id="168" />
    <tile x="2" y="8" id="99" />
    <tile x="3" y="0" id="10" />
    <tile x="3" y="1" id="17" />
    <tile x="3" y="2" id="17" />
    <tile x="3" y="3" id="162" />
    <tile x="3" y="4" id="0" />
    <tile x="3" y="5" id="0" />
    <tile x="3" y="6" id="0" />
    <tile x="3" y="7" id="0" />
    <tile x="3" y="8" id="117" />
    <tile x="4" y="0" id="11" />
    <tile x="4" y="1" id="17" />
    <tile x="4" y="2" id="163" />
    <tile x="4" y="3" id="168" />
    <tile x="4" y="4" id="168" />
    <tile x="4" y="5" id="0" />
    <tile x="4" y="6" id="143" />
    <tile x="4" y="7" id="0" />
    <tile x="4" y="8" id="101" />
    <tile x="5" y="0" id="9" />
    <tile x="5" y="1" id="169" />
    <tile x="5" y="2" id="17" />
    <tile x="5" y="3" id="0" />
    <tile x="5" y="4" id="143" />
    <tile x="5" y="5" id="0" />
    <tile x="5" y="6" id="143" />
    <tile x="5" y="7" id="0" />
    <tile x="5" y="8" id="99" />
    <tile x="6" y="0" id="45" />
    <tile x="6" y="1" id="0" />
    <tile x="6" y="2" id="0" />
    <tile x="6" y="3" id="0" />
    <tile x="6" y="4" id="162" />
    <tile x="6" y="5" id="162" />
    <tile x="6" y="6" id="168" />
    <tile x="6" y="7" id="0" />
    <tile x="6" y="8" id="135" />
    <tile x="7" y="0" id="28" />
    <tile x="7" y="1" id="0" />
    <tile x="7" y="2" id="0" />
    <tile x="7" y="3" id="169" />
    <tile x="7" y="4" id="17" />
    <tile x="7" y="5" id="17" />
    <tile x="7" y="6" id="169" />
    <tile x="7" y="7" id="0" />
    <tile x="7" y="8" id="135" />
    <tile x="8" y="0" id="68" />
    <tile x="8" y="1" id="169" />
    <tile x="8" y="2" id="17" />
    <tile x="8" y="3" id="17" />
    <tile x="8" y="4" id="17" />
    <tile x="8" y="5" id="168" />
    <tile x="8" y="6" id="162" />
    <tile x="8" y="7" id="0" />
    <tile x="8" y="8" id="118" />
    <tile x="9" y="0" id="69" />
    <tile x="9" y="1" id="162" />
    <tile x="9" y="2" id="17" />
    <tile x="9" y="3" id="163" />
    <tile x="9" y="4" id="169" />
    <tile x="9" y="5" id="169" />
    <tile x="9" y="6" id="0" />
    <tile x="9" y="7" id="169" />
    <tile x="9" y="8" id="136" />
    <tile x="10" y="0" id="70" />
    <tile x="10" y="1" id="17" />
    <tile x="10" y="2" id="44" />
    <tile x="10" y="3" id="0" />
    <tile x="10" y="4" id="143" />
    <tile x="10" y="5" id="0" />
    <tile x="10" y="6" id="168" />
    <tile x="10" y="7" id="0" />
    <tile x="10" y="8" id="100" />
    <tile x="11" y="0" id="27" />
    <tile x="11" y="1" id="0" />
    <tile x="11" y="2" id="163" />
    <tile x="11" y="3" id="143" />
    <tile x="11" y="4" id="17" />
    <tile x="11" y="5" id="0" />
    <tile x="11" y="6" id="162" />
    <tile x="11" y="7" id="0" />
    <tile x="11" y="8" id="119" />
    <tile x="12" y="0" id="10" />
    <tile x="12" y="1" id="169" />
    <tile x="12" y="2" id="17" />
    <tile x="12" y="3" id="17" />
    <tile x="12" y="4" id="163" />
    <tile x="12" y="5" id="163" />
    <tile x="12" y="6" id="143" />
    <tile x="12" y="7" id="0" />
    <tile x="12" y="8" id="99" />
    <tile x="13" y="0" id="27" />
    <tile x="13" y="1" id="0" />
    <tile x="13" y="2" id="143" />
    <tile x="13" y="3" id="162" />
    <tile x="13" y="4" id="0" />
    <tile x="13" y="5" id="143" />
    <tile x="13" y="6" id="80" />
    <tile x="13" y="7" id="0" />
    <tile x="13" y="8" id="142" />
    <tile x="14" y="0" id="28" />
    <tile x="14" y="1" id="169" />
    <tile x="14" y="2" id="0" />
    <tile x="14" y="3" id="143" />
    <tile x="14" y="4" id="168" />
    <tile x="14" y="5" id="83" />
    <tile x="14" y="6" id="11" />
    <tile x="14" y="7" id="0" />
    <tile x="14" y="8" id="124" />
    <tile x="15" y="0" id="67" />
    <tile x="15" y="1" id="42" />
    <tile x="15" y="2" id="60" />
    <tile x="15" y="3" id="130" />
    <tile x="15" y="4" id="148" />
    <tile x="15" y="5" id="151" />
    <tile x="15" y="6" id="149" />
    <tile x="15" y="7" id="132" />
    <tile x="15" y="8" id="85" />
  </OurTiles>
  <Upgrades>
    <Armor_Survival id="0" x="120" y="120" />
    <Armor_Endurance id="1" x="360" y="120" />
    <Armor_Malefic id="2" x="600" y="120" />
    <Luck_Stroke id="3" x="1560" y="120" />
    <Damage_Violent id="4" x="1320" y="120" />
    <Magic_Occultism id="5" x="1080" y="120" />
    <Agility_Dagger id="6" x="840" y="120" />
  </Upgrades>
</level>```

**PROBLEM ONE**: The buttons/talents load at the start of the game, even if I don't press I! The background used for the window only appears when I press I. So only the background works -.-

**PROBLEM TWO**: The buttons all have the same placement. They are all placed on the top left of the screen... What the hel!

Thanks :p

(reopucino) #2

solve problem one : did you has check world class?? I think you put that button at world class, so that button has apear when you call world class


(Ultima2876) #3
  1. That’s because you’re creating the Talent_TreeParser here:
private var parser:Talent_TreeParser = new Talent_TreeParser(TALENTS);

The constructor gets called as soon as the object is instantiated (with new), and in that constructor you call the loadInventory function which creates your other entities. This will happen as soon as a Talent_Tree object is created, which I’m guessing is in a similar place in your World class (at the top of the file with the variable declerations). To fix this, you probably want to load the xml (and thus create all the objects) when the Talent_TreeParser is added instead:

override public function added():void //HAS BEEN ADDED
	{
		loadInventory(xml); //don't want this in the constructor!
	}
  1. Do your Armor_Survival etc classes call super(x, y); or this.x = x; this.y = y; in their constructors? If you don’t do one of these, it will not pass the x/y value to the Entity base class and they will appear at 0, 0.

(John Andersson) #4

Thanks, I fixed the appearance by doing this:

	[Embed(source="../Panels/talents.oel", mimeType="application/octet-stream")]private static const TALENTS:Class;
	
	public function Talent_TreeParser()
	{
		_tiles = new Tilemap(Assets.INVENTORY_TILESET, 2160, 1200, 120, 120);
		graphic = _tiles;
		
		type = "talents";
	}

	override public function added():void //HAS BEEN ADDED
	{
		loadInventory(TALENTS);
	}

And fixed the positioning like this

	public function Agility_Dagger(p:Point) 
	{
		x = p.x;
		y = p.y;
           etc
            }

Thank you SO MUCH! I love you :slight_smile: