Flash AS3 Array and Sorting

Go To StackoverFlow.com

0

My problem is I Have an isometric grid, just based on the X,Y values: Each time you click a button for instance road than this item is pushed to an array()

 //
 addChild(roadTiles);
 gameAssets.push(roadTiles);
 arrangeAssets();
 //

My arrangeAssets method just checks the array "no point posting as it fails most of the time.gennerally some items update but not all. So im hoping some one has a solution to check X,Y value then setChildIndex of Array. I have looked, keep getting reffered to 3rd party as3 engines which im not interested in as i want to make my own and have better understanding.

enter image description here

or is this not possible as im only using an X,Y grid

    arrangeAssets();{
var i:int = gameAssets.length;
while(i--){
    if (getChildIndex(sortedItems[i]) != i) {
        gameAssets.sortOn("y", Array.NUMERIC);
        setChildIndex(gameAssets[i], i);
    }
    }

I only add the items on top of the grass grid into the array.

2012-04-05 16:03
by joshua
I think you are going to need to take another stab at explaining what you are looking to do. Start with posting your current arrangeAssets code, and give an example of a new roatTiles instance you want to push into gameAssets and how it should be handled - sberry 2012-04-05 16:10
just updated info - joshua 2012-04-05 16:26
I'm still having a hard time understanding, can you specify what you want the sorting to do vs what it is doing? or is this not a sorting related question - Daniel 2012-04-05 16:30
do you want the item you click on to go to the top of the display list, based on the array that you're trying to sort - Daniel 2012-04-05 16:32
if i was to add a buiding on this grid, than directly behind it add another what happens is the second building overlaps the first building sometimes, or some other times it dissapears until i add another structure than it re-appear - joshua 2012-04-05 16:36
I think I get now. you have a tile like the semaphore, that gets covered by the street above - Daniel 2012-04-05 16:38
correct, if i had buildings instead of roads you could really see the difference but i deleted them as im messing with all the roads tiles firs - joshua 2012-04-05 16:41


1

assuming that the left most point is x: 0, y:0 the tiles are covering each other going from a high x and low y to a low x and high y values.

I would add a weight variable (call it what you like), and calculate it from (totalx - x) * y

this would have the top-most corner have a value of 0, and the bottom most have the highest value. every row (diagonal really) would be the same.

Then sort on the weight and you have a list of items in the relative z stack.

this doesn't really address the setchildIndex, because you can't have two items there. a hack would be to remove all and add them back in the order. (you could have a temp child, that you add as children to, that way you don't have to remove the children as it is done automatically)

I found this image that shows the weight as _yfrom http://www.kirupa.com/developer/isometric/depth_sorting.htm

enter image description here enter image description here

the _y is the same because (totalx - x) * y values horizontally are the same

2012-04-05 16:48
by Daniel
sounds interesting but its over my head, lol i will ponder with this thought for now thanks i cant get my head around weight value? where did he come fro - joshua 2012-04-05 16:54
it is calculated, imagine the grid as a diamond, the top is at the back with the lowest weight number, and the bottom is the front with the highest - Daniel 2012-04-05 16:57
neally getting it, sorry man im not a programme - joshua 2012-04-05 17:00
updated answer with image - Daniel 2012-04-05 17:01
Iv read that several times its not the theory thats making me ponder its the formula, how to impliment this method by adding this"weight" - joshua 2012-04-05 17:03
totalx is the number of rows(or columns) along the x axis, I had x - totalx instead of totalx - x I edited the answe - Daniel 2012-04-05 17:06
i chose your answer im understanding a bit thanks for the help, its now time to implement this to practis - joshua 2012-04-05 17:11
Ads