-
Z-Order/Child Indexing Problem
Hi guys
Just a quick one regarding z-indexing/child indexing.
I'm in a bit of a problem here!!
Supposing I create a instance of a tree sprite and place it at 250x, 300y.
Then I create 20 instances of a sphere. The spheres randomly move around, infront of and behind the static tree, but everything is indexes itself correctly so things move behind and in front of each other.
I've heard this is called z-indexing, and it needs to be done on every frame update but I'm having a lot of difficulty doing this in AS3. Does anyone have any hints, algorithms/solutions on how to solve this?
Any help would be appreciated.
Thank you!
-
When you use addChild, the new child is added in front of any other children in the same parent.
You can use swapChildren to change the depths of two display children, or you could use addChildAt to specify a depth to assign to a child. Be careful, only values between 0 and numChildren-1 are valid depths.
Can you be more specific about what you're having trouble with?
-
OK to be more specific, I'm trying to get guys to move in and out of buildings. But they can walk around on the streets outside too. The guys walk around randomly and enter buildings randomly. Once they're inside, the z-index doesn't matter and can be set to 1 (so long as the building sprite is set to 2 or higher). But when they're outside, they need to be z-indexed according to their Y position on the pavement.
The number of guys is random every time.
I think this sort of thing is referred to as depth sorting. It was easy in AS2 but in AS3 its quite a challenge!
I hope that explains it a bit better.
-
I see. You basically want to have things automatically z sorted based on their y value. I suppose you could sort them by y each frame and mess with their depths, but that doesn't seem like a great solution. You could just check the relative y values on any collision and swap if necessary. I don't like that one either.
This is an interesting problem. I might actually draw everything into a Bitmap instead of using DisplayObjects directly. You could sort things by y, then draw them in the same order. That's not ideal either. I'll have to think of better solutions for a while. Please post if you come up with something.
-
If you can come up with a solution, then that would be awesome!!!
Any method based upon the Y value of each instance is perfect! Thats exactly what I need. Cos once the guys move up in the Y direction and hit a certain value, they're off the pavement and inside a building.
Just a little info about how my code is structured. I use an empty root movieclip called "game" which I add 20 or 30 guy instances and a building instance to. This is so I can move the "stage" around and scale it, etc. So every call I make has to include the parent MC. e.g: game.guy1, game.guy2, game.building, etc
Regarding possible solutions, I've found two z-sort methods on other sites:
http://www.kirupa.com/forum/showthread.php?p=1897023
I tried that and it didn't work for some reason! All my instances just disappeared!
http://mike.newgrounds.com/news/post/59329
This looks really cool, but I think it needs a bit of explaining cos I'm not too sure how it works.
I've tried a few of my own methods (mostly on enter frame events where it tries to analyse the Y value and sort things, but I couldn't get it work properly)
Anyway if you manage to find a solution, I would be really grateful!
Thank you!
-
That second one is basically the same approach as sorting on enter frame, but he wrapped it in some nice classes. I was hoping to avoid sorting on each frame somehow. It should more or less work, though. I could clean up that code and post that. With 30 sorted instances, the overhead would be minimal.
Do all your guys move at once, or do they take turns?
Edit. Just looked at the first one. That should work fine. Did you add a call to arrange in an enter frame listener?
Last edited by 5TonsOfFlax; 08-20-2008 at 07:01 PM.
-
That first approach is what I had in mind - it should go quickly because the Array.sort method is running machine code instead of script, and I'm sure it's using a much better algorithm than bubble sort (woof!).
-
If it uses anything other than quicksort or mergesort, someone should be shot. Maybe me if I forgot another nlogn sort.
-
That second one is basically the same approach as sorting on enter frame, but he wrapped it in some nice classes. I was hoping to avoid sorting on each frame somehow. It should more or less work, though. I could clean up that code and post that. With 30 sorted instances, the overhead would be minimal.
Oh man that would be appreciated!
Do all your guys move at once, or do they take turns?
No they all move at once, and are pretty much constantly on the move.
Edit. Just looked at the first one. That should work fine. Did you add a call to arrange in an enter frame listener?
Yes I used an on enter frame event which called that function. All my instances just disappeared within a second! I had to edit it, a little but heres code I used.
PS: "game" is an empty container movieclip
PHP Code:
var sortedItems:Array = new Array();
var gameFloor = 310;
var buildings:Buildings = new Buildings();
buildings.name = "buildings";
buildings.x = 0;
buildings.y = gameFloor;
sortedItems.push(buildings);
game.addChild(buildings);
for (var i:int = 1; i <= noOfCitizens; i++)
{
var person:Person = new Person();
person.name = "person"+i;
sortedItems.push(person);
game.addChild(person);
}
//following is called every frame update
//using an OnEnterFrame event listener
function arrange():void {
sortedItems.sortOn("y", Array.NUMERIC);
var i:int = sortedItems.length;
while(i--){
if (game.getChildAt(i) != sortedItems[i]) {
game.setChildIndex(sortedItems[i], i);
}
}
}
Like I say it just went all screwy and made everything disappear!
Each person object moves independently upon being added to the stage.
-
I don't see anything obviously wrong with that code. You're not getting any errors?
I'll try to play with this if I have spare time today.
-
Originally Posted by 5TonsOfFlax
I'll try to play with this if I have spare time today.
Cool! Thanks!
-
I haven't had time for this yet. I'm still at work. Don't wait for me to bail you out.
I'll probably get to it tonight. But if not, it's probably never.
-
OK I think I've solved it using the first method. I found out what I was doing wrong. The container mc "game" had some test vector graphics in it, and the algorithm was moving the container quite slowly over time to the front (in front of all the instances - therefore making them disappear one by one). I don't know why, but that's the reason.
Anyway thanks for offering to look at it. I'm really sorry if I disturbed you at work (its late at night here in Britain - past midnight to be precise, so I kinda forgot about the timezone, mainly due to tiredness).
-
Oh no, you didn't disturb me. If I wanted not to be disturbed by this board, I wouldn't surf it all the time. Congrats on getting it to work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|