displayList enigma

15 posts

Flag Post

I can’t find a solution for this displayList problem, for a whole week already :O

So let’s put many minds on it.

I decided to make a tileMap editor with displayList implemented, everything seems to be working well, except for one thing.

After each tile is placed I update display list with this function:

function updateIndex():void
{
	
// This will get the last placed tile

	if(realTilesContainer.numChildren > 0) var tempTile1:DisplayObject = realTilesContainer.getChildAt(realTilesContainer.numChildren - 1);

//Then I loop through all tileContainer items
	
		for(var a:int = 0; a < realTilesContainer.numChildren; a++)
		{

				var tempTile2:DisplayObject = realTilesContainer.getChildAt(a);


// This is the logic of changing tile indexes			

					if(tempTile1.y + tempTile1.height < tempTile2.y + tempTile2.height && realTilesContainer.getChildIndex(tempTile1) > realTilesContainer.getChildIndex(tempTile2) && tempTile1.hitTestObject(tempTile2))
					{

// If currently placed tile's lower bottom is higher than any nearby tile lower 
//bottom I set current tile's index to be lower than tempTile2 index

						realTilesContainer.setChildIndex(tempTile1, realTilesContainer.getChildIndex(tempTile2));
					}

		}


}

But here is one thing that doesn’t work:

If I place 1 tile, then place 2nd tile above and if I place 3rd tile in the middle between these two, the upper tile will have a higher index, 1st. example:

But I want to make it like this, 2nd. example:

You can try it here

There must be some way to achieve this, if you have any suggestions, please write them here, I’m out of ideas.

There must be some other way to update indexes

 
Flag Post

Put every tile into some array and then sort it by the ‘y’ property.

tilesArray.sortOn("y", Array.NUMERIC);
for each(var tile:Tile in tilesArray) {
  addChild(tile);
}

Something like this. Not the ‘techy’ way, but convenient. Does this help?

 
Flag Post
Originally posted by BrainStormer:

Put every tile into some array and then sort it by the ‘y’ property.

tilesArray.sortOn("y", Array.NUMERIC);
for each(var tile:Tile in tilesArray) {
  addChild(tile);
}

Something like this. Not the ‘techy’ way, but convenient. Does this help?

Unfortunately in this scenario sorting only on y coordinate is not good enough, because there are high objects, so even if they have a lower y coordinate, their bottom may have a bigger y coordinate than a smaller object’s bottom y.

But your example gave me an idea. But how could I add additional parameter to each tile that I push into array, for example:


//if I have an int variable

var coord:int = 5;
var tile:MovieClip;

// How would I add this var to be additional parrameter in array, connected to current object

tileArray.push(tile // And here somehow add aditional parameter, named "coordinate" with coord value, so I could sort array by "coordinate"

How could I do this?

 
Flag Post

The lower edge is the thing you’re interested in?
Write a getter that exposes the “bottom” porperty of the rectangle as a property of the element, sortOn that.

 
Flag Post
Originally posted by NineFiveThree:

The lower edge is the thing you’re interested in?
Write a getter that exposes the “bottom” porperty of the rectangle as a property of the element, sortOn that.

Yes that’s what I’m thinking about, each time I add a tile to array I will add additional parameter “coord” which will be var coord:int = object.y + object.height.

But would this be correct?:


var coord:int = tile.y + tile.height;
var tile:Tile1 = new Tile1();

tileArray.push({object:tile, yPos:coord});

Then I will sort array on “yPos” parameter, and then will skim through all tiles and set their indexes, this may work, but is my code above to add object and to set his “yPos” parameter correct? please someone tell me.

 
Flag Post
Originally posted by AMD_Paulius_J:

But would this be correct?:


var tile:Tile1 = new Tile1();
var coord:int = tile.y + tile.height;

tileArray.push([tile, coord]);
tileArray[tileArray.length]=[tile,coord]; //This means the same thing, but I think it's faster.

I think that’s what you’re trying to do???
You will get an array like:
[[tile1,2],[tile2,4],[tile3,7],[tile4,10]];
So, when you're looping through, tileArray[i][0] will be the tile, and tileArray[i][1] will be the coord value.
 
Flag Post
I think that’s what you’re trying to do???
You will get an array like:
[[tile1,2],[tile2,4],[tile3,7],[tile4,10]];
So, when you’re looping through, tileArray[i]0 will be the tile, and tileArray[i]1 will be the coord value.

Yes it will be something like that, but I am giving additional names, so tileArray[O].tile will be the actual object, and tileArray[O].coord will be tile’s bottom y position, I will sort everything by bottom y position and then update, it should work.

EDIT: I could not write zero inside [] it was giving me this 0

 
Flag Post
Originally posted by AMD_Paulius_J:

But would this be correct?:


var coord:int = tile.y + tile.height;
var tile:Tile1 = new Tile1();

tileArray.push({object:tile, yPos:coord});

No, that code won’t work at all.
You access tile before you declare it. It would also not make this a property which you could assign to sortOn.

As I said, write a get function.

 
Flag Post
Originally posted by AMD_Paulius_J:
I think that’s what you’re trying to do???
You will get an array like:
[[tile1,2],[tile2,4],[tile3,7],[tile4,10]];
So, when you’re looping through, tileArray[i][0] will be the tile, and tileArray[i][1] will be the coord value.

Yes it will be something like that, but I am giving additional names, so tileArray[O].tile will be the actual object, and tileArray[O].coord will be tile’s bottom y position, I will sort everything by bottom y position and then update, it should work.

EDIT: I could not write zero inside [] it was giving me this 0

Oh. So they’re objects in the array. What you had before would probably work then, just make sure you put var tile above var coord.
I don’t know how you’d go about sorting it though, Refer to above post.
 
Flag Post

No, that code won’t work at all.
You access tile before you declare it. It would also not make this a property which you could assign to sortOn.

As I said, write a get function.

Hmm, but I’m not so sure how to use a get function, maybe to return a bottom y coordinate, but I can already access that, because tile is being pushed into array from the same function so it’s easier to set a new var with bottom coordinate.

Well i updated everything, and it seems to be working, thank you all for suggestions it really helped to get the sorting idea.

Oh. So they’re objects in the array. What you had before would probably work then, just make sure you put var tile above var coord.
I don’t know how you’d go about sorting it though, Refer to above post.

I’m sorting on yPos:int, actually it’s not so complicated as it looked at the beginning.


//First get a needed coordinate

var coord:int = tile.y + tile.height;

// Then push tile into array and add yPos property

tileArray.push({object:tile, yPos:coord});

// And then sort by yPos

tileArray.sortOn("yPos", Array.NUMERIC);

That’s it, here is an updated version.

 
Flag Post

More Lure or Die. Nice!

 
Flag Post

Creating a map editor takes time but it’s definitely worth it, now creating a level is a pleasure. I created levels manually before, by specifying coordinates:)

 
Flag Post

A get function would calculate that value and return it, no matter if you cache the values or not, it’s going to come in handy, because in an editor, it’s very likely that you have to recalculate that value again.

 
Flag Post
Originally posted by NineFiveThree:

A get function would calculate that value and return it, no matter if you cache the values or not, it’s going to come in handy, because in an editor, it’s very likely that you have to recalculate that value again.

Hmm, now that sounds like a good point, but I’m not familiar with get/set functions so I will stick with an old methods, at least for this map editor. I think current method should be ok, because it’s only 1 additional variable for each tile.

 
Flag Post
Originally posted by AMD_Paulius_J:

but I’m not familiar with get/set functions.

You could either repeat that froever or actually read what I write:
it’s just a function that returns the result of the calculation.