Help pushing xml data into an array

10 posts

Flag Post

So I am using a FriendsofED flash book, and trying to transplant sections for my own game. I cannot even get what seems to be a simple portion of code to work.
So what I’m doing here is just setting up XML data as a static var, then having a function read and trace the output.

Here’s the XML sheet

package com.efg.games.zombiegame  
{
	public class TilesheetDataXML 
	{
	public static var XMLData:XML=
			
			<tilesheet>
			
			<tile id="0" name="building" type="structure"></tile>
			<tile id="1" name="building" type="structure"></tile>
			<tile id="2" name="building" type="structure"></tile>
			<tile id="3" name="building" type="structure"></tile>
			<tile id="4" name="landscape" type="background"></tile>
			<tile id="5" name="landscape" type="background"></tile>
			<tile id="6" name="landscape" type="background"></tile>
			<tile id="7" name="landscape" type="background"></tile>
			
			</tilesheet>;

	}
}
	

And, here is the class which is supposed to read it and trace out the arrays. I have copied it exactly to the original code(except for names), the original code works when I run it.

package com.efg.games.zombiegame  
{
	import flash.display.Sprite;
	
	public class Main extends Sprite
	{
		public static const TILE_BUILDING:int = 0;
		public static const TILE_LANDSCAPE:int = 1;
		
		private var buildingFrames:Array;
		private var landscapeFrames:Array;
		private var tileSheetData:Array;
		
		public function Main() 
		{
			initTileSheetData();
			trace("tileSheetData.length=" + tileSheetData.length);
			trace("buildingFrames.length=" + buildingFrames.length);
			trace("landscapeFrames.length=" + landscapeFrames.length);
		}
		
		private function initTileSheetData():void
		{
			buildingFrames = [];
			landscapeFrames = [];
			tileSheetData = [];
			var numberToPush:int = 99;
			var tileXML:XML = tileSheetDataXML.XMLData;
			var numTiles:int = tileXML.tile.length();
			for (var tileNum:int = 0; tileNum < numTiles; tileNum++) 
			{
			if (String(tileXML.tile[tileNum].@type == "structure")
				{
				numberToPush = TILE_BUILDING;
				buildingFrames.push(tileNum);
				}
			else if (String(tileXML.tile[tileNum].@type == "background")
				{
				numberToPush = TILE_LANDSCAPE;
				landscapeFrames.push(tileNum);
				}
			}
			tileSheetData.push(numberToPush);
		}
	}
}

I’m really stumped trying to figure this out, I don’t know what I’m changing that “breaks the chain”. I do not get ANY errors, there is simply no trace output. I tried copying the code then editing it, I tried writing it fresh, neither works. Here’s the original code(well sort of, the author did not provide the first iteration with only the XML data and InitTileSheetData function)

/**
* ...
* @author Jeff Fulton
* @version 0.1
* //file name TileSheetDataXML.as
*/

package com.efg.games.notanks {
	
	
	public class TilesheetDataXML {
	
		public static var XMLData:XML=
		
<tilesheet>
	
		<tile id="0" name="road" type="walkable"></tile>
		<tile id="1" name="player" type="sprite"></tile>
		<tile id="2" name="player" type="sprite"></tile>
		<tile id="3" name="player" type="sprite"></tile>
		<tile id="4" name="player" type="sprite"></tile>
		<tile id="5" name="player" type="sprite"></tile>
		<tile id="6" name="player" type="sprite"></tile>
		<tile id="7" name="player" type="sprite"></tile>
		
		<tile id="8" name="player" type="sprite"></tile>
		<tile id="9" name="enemy" type="sprite"></tile>
		<tile id="10" name="enemy" type="sprite"></tile>
		<tile id="11" name="enemy" type="sprite"></tile>
		<tile id="12" name="enemy" type="sprite"></tile>
		<tile id="13" name="enemy" type="sprite"></tile>
		<tile id="14" name="enemy" type="sprite"></tile>
		<tile id="15" name="enemy" type="sprite"></tile>
		
		<tile id="16" name="enemy" type="sprite"></tile>
		<tile id="17" name="explode1" type="sprite"></tile>
		<tile id="18" name="explode2" type="sprite"></tile>
		<tile id="19" name="explode3" type="sprite"></tile>
		<tile id="20" name="ammo" type="sprite"></tile>
		<tile id="21" name="missile" type="sprite"></tile>
		<tile id="22" name="lives" type="sprite"></tile>
		<tile id="23" name="goal" type="sprite"></tile>
		
		
		<tile id="24" name="blueblock1" type="nonwalkable"></tile>
		<tile id="25" name="blueblock2" type="nonwalkable"></tile>
		<tile id="26" name="blueblock3" type="nonwalkable"></tile>
		<tile id="27" name="blueblock4" type="nonwalkable"></tile>
		<tile id="28" name="blueblock5" type="nonwalkable"></tile>
		<tile id="29" name="blueblock6" type="nonwalkable"></tile>
		<tile id="30" name="blueblock7" type="nonwalkable"></tile>
		<tile id="31" name="blueblock8" type="nonwalkable"></tile>
		
		<smallexplode tiles="17,18,17"></smallexplode>
		<largeexplode tiles="17,18,19,18,17"></largeexplode>
		
</tilesheet>;
	
	} // end class
}// end package
package com.efg.games.notanks
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import com.efg.framework.TileSheet;
	
	/**
	 * ...
	 * @author Jeff Fulton
	 */
	public class GameDemo extends Sprite
	{
		
		public static const TILE_WALL:int = 0;
		public static const TILE_MOVE:int = 1
		public static const SPRITE_PLAYER:int = 2;
		public static const SPRITE_GOAL:int = 3;
		public static const SPRITE_AMMO:int = 4;
		public static const SPRITE_ENEMY:int = 5;
		public static const SPRITE_EXPLODE:int = 6;
		public static const SPRITE_MISSILE:int = 7;
		public static const SPRITE_LIVES:int = 8;
		
		private var playerFrames:Array;
		private var	enemyFrames:Array;
		private var	explodeFrames:Array;
		private var	tileSheetData:Array;
		
		private var missileTiles:Array=[];
		private var explodeSmallTiles:Array;
		private var explodeLargeTiles:Array;
		private var ammoFrame:int;
		private var livesFrame:int;
		private var goalFrame:int;
		
		private var tileWidth:int = 32;
		private var tileHeight:int = 32;
		private var mapRowCount:int = 15;
		private var mapColumnCount:int = 20;
		
		private var level:int = 1;
		private var levelTileMap:Array;
		private var levelData:Level;
		private var levels:Array = [undefined,new Level1()]
		
		private var canvasBitmapData:BitmapData=new BitmapData(tileWidth * mapColumnCount, tileHeight * mapRowCount, true, 0x00000000);
		private var canvasBitmap:Bitmap = new Bitmap(canvasBitmapData);
		private var blitPoint:Point = new Point();
		private var tileBlitRectangle:Rectangle = new Rectangle(0, 0, tileWidth, tileHeight);
		
		
		//***** Flex ***** 
		//private var tileSheet:TileSheet= new TileSheet(new Library.TankSheetPng().bitmapData, tileWidth, tileHeight);
		//***** End Flex *****
		
		//***** Flash IDE *****
		private var tileSheet:TileSheet = new TileSheet(new TankSheetPng(0,0), tileWidth, tileHeight);
		//***** End Flash IDE *****
		
		
		public function GameDemo() 
		{
			initTileSheetData();
		
			trace("tileSheetData.length=" + tileSheetData.length);
			trace("playerFrames.length=" + playerFrames.length);
			trace("enemyFrames.length=" + enemyFrames.length);
			trace("missileTiles.length=" + 	missileTiles.length);
			trace("explodeSmallTiles.length=" + explodeSmallTiles.length);
			trace("explodeLargeTiles.length=" + explodeLargeTiles.length);
			
			readBackGroundData();
			readSpriteData();
			drawLevelBackGround();
			addChild(canvasBitmap);
		}
		
		
		private function initTileSheetData():void {
			playerFrames = [];
			enemyFrames = [];
			tileSheetData = [];
			var numberToPush:int = 99;
			var tileXML:XML = TilesheetDataXML.XMLData;
			var numTiles:int = tileXML.tile.length();
			for (var tileNum:int = 0; tileNum < numTiles; tileNum++) {
				if (String(tileXML.tile[tileNum].@type) == "walkable") {
					//tileSheetData.push(TILE_MOVE);
					numberToPush = TILE_MOVE;
				}else if (String(tileXML.tile[tileNum].@type) == "nonwalkable") {
					//tileSheetData.push(TILE_WALL);
					numberToPush = TILE_WALL;
				}else if (tileXML.tile[tileNum].@type == "sprite") {
					
					switch(String(tileXML.tile[tileNum].@name)) {
					
						case "player":
							//tileSheetData.push(SPRITE_PLAYER);
							numberToPush = SPRITE_PLAYER;
							playerFrames.push(tileNum);
							break;
						
						case "goal":
							//tileSheetData.push(SPRITE_GOAL);
							numberToPush = SPRITE_GOAL;
							goalFrame = tileNum;
							break;
							
						case "ammo":
							//tileSheetData.push( SPRITE_AMMO );
							numberToPush = SPRITE_AMMO;
							ammoFrame = tileNum;
							break;
						
						case "enemy":
							//tileSheetData.push(SPRITE_ENEMY);
							numberToPush = SPRITE_ENEMY;
							enemyFrames.push(tileNum);
							break;
						
						case "lives":
							//tileSheetData.push(SPRITE_LIVES);
							numberToPush = SPRITE_LIVES;
							livesFrame = tileNum;
							break;
						case "missile":
							//tileSheetData.push(SPRITE_LIVES);
							numberToPush = SPRITE_MISSILE;
							missileTiles.push(tileNum);
							break;
					}
					
				}
				tileSheetData.push(numberToPush);
				
				
		
			}
			explodeSmallTiles = tileXML.smallexplode.@tiles.split(",");
			explodeLargeTiles = tileXML.largeexplode.@tiles.split(",");
			
		}
		
		private function readBackGroundData():void {
			levelTileMap = [];
			levelData = levels[level];
			levelTileMap = levelData.backGroundMap;
		}
		
		private function readSpriteData():void {
			//place holder for reading sprite data and placing sprites on the screen
		}
		
		private function drawLevelBackGround():void {
			canvasBitmapData.lock();
			var blitTile:int;
			for (var rowCtr:int=0;rowCtr<mapRowCount;rowCtr++) {
				for (var colCtr:int = 0; colCtr < mapColumnCount; colCtr++) {
					
					blitTile = levelTileMap[rowCtr][colCtr];
					
					tileBlitRectangle.x = int(blitTile % tileSheet.tilesPerRow) * tileWidth;
					tileBlitRectangle.y = int(blitTile / tileSheet.tilesPerRow) * tileHeight;
					
					blitPoint.x=colCtr*tileHeight;
					blitPoint.y = rowCtr * tileWidth;
					
					canvasBitmapData.copyPixels(tileSheet.sourceBitmapData,tileBlitRectangle, blitPoint);
				}
			}
			canvasBitmapData.unlock();
		}
		
		
	}
	
}

 
Flag Post
var numTiles:int = tileXML.tile.length();
trace(numTiles);

What does that trace?

 
Flag Post
if (String(tileXML.tile[tileNum].@type == "structure")
(...)
else if (String(tileXML.tile[tileNum].@type == "background")

Both of those are missing a parenthesis at the end.

 
Flag Post

var numTiles:int = tileXML.tile.length();
trace(numTiles);
What does that trace?

It traces nothing. So I guess the TilesheetDataXML class isn’t linking any info to Main? I’ve reread the chapter but can’t find what I missed.. perhaps there was an issue with mappy when exporting, but I don’t even think that is involved here. I guess I will read some web sources to better understand what this guy is doing.

Thanks for catching that error, however it still didn’t lead to output being traced. This is the 3rd time I’ve gone through this so I know at least one of the other times (when I copied code straight, just changing variables) I didn’t miss that parentheses.

 
Flag Post

What IDE are you using?
I just copy pasted your first two classes, added the parenthesis and got this:

tileSheetData.length=1
buildingFrames.length=8
landscapeFrames.length=0

 
Flag Post

I’m using flash professional… at least I know the lack of output it is a software issue, or a settings issue (possibly with this project). I’ll get on that, then try fixing that output since that’s not what I want :P thanks for the help.

 
Flag Post

Try creating a new projeand copy over the code. If you don’t seee any output, download FlashDevelop and give it a try.

 
Flag Post

Nice, starting a new project got it working. Now hopefully the rest of this chapter will go smooth.

 
Flag Post

Welcme to the world of CS.

That’s why I stopped usign it. It’s full of WTFs.

 
Flag Post

I have encounted a flash wtf. Will post soon if I can’t solve it…

edit: I think I know what’s going on, I just need to figure out how to fix it.