Hello fellows!
So, I’m having this weird problem and I know you guys are the best and can help me solving this.
What’s happening is that I have to develop a pathfinder into a game. I found a class that makes the pathfinder, great. But I needed to change it because in this project I can’t use any code inside flash it’s completely OOP. And I got to load my grid from a external xml class.
So here is the problem: I can´t call a function in the Main class to add the grid as a child only after it has being filled with xml info.
Here are the GridClass and the Main Class
Main
package com.pathfinder
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.events.*;
/**
* ...
* @author Derick Troia
*/
public class Main extends MovieClip
{
var avatar = new Player();
var myGrid = new GridClass();
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
myGrid.loadGrid("cemetery");
avatar.init(stage.stageWidth / 2 - avatar.width / 2, stage.stageHeight / 2 - avatar.height / 2);
}
public function addMyChild()
{
addChild(avatar);
addChild(myGrid);
}
}
}
GridClass
package com.pathfinder
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.Event;
import flash.net.*;
import com.pathfinder.*;
;
/**
* ...
* @author Derick Troia
*/
public class GridClass extends MovieClip
{
public var grid:Array = new Array;
public var rows:uint;
public var columns:uint;
public var xmlData:XML = new XML();
private var xmlLoader:URLLoader = new URLLoader();
public var cenario:String;
public var pathFinder:Path;
public var endCell:Object;
public function GridClass()
{
xmlLoader.load(new URLRequest('../../Grid.xml'));
}
public function loadGrid(_cenario:String)
{
cenario = _cenario;
xmlLoader.addEventListener(Event.COMPLETE, fillGrid); //recebe e:event
}
public function fillGrid(e:Event)
{
xmlData = new XML(e.target.data);
trace( " --------------XML DATA--------------- " );
trace(xmlData);
trace( " --------------GRIDDATA--------------- " );
if (xmlData.Grids.Grid.@name == cenario)
{
for each(var cell:XML in xmlData.Grids.Grid.cell)
{
grid.push(cell.@value)
}
}
trace(grid);
drawGrid(grid,this,"n",xmlData.Grids.Grid.@tileSize,xmlData.Grids.Grid.@tileSize);
}
function drawGrid(grid:Array, mc:MovieClip, linkage:String, w:Number, h:Number):void
{
pathFinder = new Path(grid);
endCell = grid[xmlData.Grids.Grid.@rows][xmlData.Grids.Grid.@columns];
var numCell:int;
for (var i:Number = 0; i < grid.length; i++)
{
numCell ++;
for (var j:Number = 0; j < grid*.length; j++)
{
var cell:Tile
if (grid*[j].t == 1)
{
cell = new Tile(grid,pathFinder,i,j,true);
}
else
{
cell = new Tile(grid,pathFinder,i,j,false);
}
cell.x = j*w;
cell.y = i*h;
cell.name = "cell_"+i+"_"+j;
cell.gotoAndStop(grid*[j].t);
addChild(cell);
}
trace(numCell);
if (numCell >= grid.length)
{
Main(parent).addMyChild();
}
}
}
}
}
I’m getting all sorts of weird errors the actual one is the #1009
I tried to dispatch a event what didn’t work for me ( maybe because I don’t know how to do it ). I’m waiting for someone to help me.
Thanks