If any of you guys have played fire emblem(any of them), this concept will make more sense as i struggle to articulate it:
I am making a grid-based game, and as i drag the cursor away from the ‘player’, i want the ‘line of passage’ to adjust itself to find the path of least resistance to where the cursor is at. Much like how fire emblem did there’s.
So i have an array(curTile) that correlates to the grid i have built, made up of either 1’s or 0’s, 1 being a wall, and 0 being that i can walk on the said “tile”. I also have an integer that defines the number of tiles across on said grid(tilesAcross).
What i have done is every time the cursor is on a new ‘tile’, it will run this function. So i make an array(newArray) and contain another array within that array. the contained array will contain the ‘path’, so for example, a path that goes: (right,right,right,down,right,down,down,left) would look like this:
newArray[*someIndex*] = [1,1,1,tilesAcross,1,tilesAcross,tilesAcross,-1]
So the function will start small with just the index of the player(curIndex). then it will do a check and sum up every array contained in the newArray array. If nothing sums up to the index of the cursor(indexTo), then it will push the contents of the current array into the newArray variable, then it will push the integer corresponding to the direction its checking for to the end of that. (in retrospect i see that, at least for description purposes, newArray wasnt the greatest variable name for a new Array(), haha my bad)
So essencially what i am doing is mapping out every possible ‘route’ until the ‘line’ reaches its destination.
So FINALLY (I really hope that all made sense), the problem i am having is that it all works PERFECTLY as far as basic functionality goes. And it doesnt cause any lag until the cursor is about 4-5 ‘tiles’ away. but after that it takes an exponentially longer amount of time to run through that function (more ‘tiles’ to map through in every direction, i get it), and eventually the script becomes completely unresponsive.
public var newArray:Array = new Array();
public function setPath(**curIndex**:int,**indexTo**:int,**tilesAcross**:int,tilesDown:int,**curTile**:Array):Array
{
var originalArray:Array = [];
var returnValue:Array = [];
while(newArray.length > 0)
{
newArray.pop();
}
newArray[0] = [curIndex];
for(var i=0;i<newArray.length;i++)
{
if(returnValue.length == 0)
{
originalArray = newArray*;
var tempIndex:int = 0;
for(var j=0;j<newArray*.length;j++)
{
tempIndex += newArray*[j];
}
if(tempIndex == indexTo)
{
returnValue = newArray*;
}
else
{
if(curTile[tempIndex+1] == 0) //Right
{
newArray.push(new Array());
newArray[newArray.length-1] = originalArray.slice(0);
newArray[newArray.length-1].push(1);
}
if(curTile[tempIndex+tilesAcross] == 0) //Down
{
newArray.push(new Array());
newArray[newArray.length-1] = originalArray.slice(0);
newArray[newArray.length-1].push(tilesAcross);
}
if(curTile[tempIndex-1] == 0) //Left
{
newArray.push(new Array());
newArray[newArray.length-1] = originalArray.slice(0);
newArray[newArray.length-1].push(-1);
}
if(curTile[tempIndex-tilesAcross] == 0) //Up
{
newArray.push(new Array());
newArray[newArray.length-1] = originalArray.slice(0);
newArray[newArray.length-1].push(-tilesAcross);
}
}
}
}
return returnValue as Array;
}
**I did consider adding a few parameters to the function that would stop the ‘mapping’ process from going back over itself, but then again i cant see that removing more than 25% of the lag anyway.
I know this is a lot of reading to do, so thanks for any and all input to this post.