actionscript 3 - As3: How to make movieclips with the highest Y position appear in front? -


How can I create a movieclip on my forum (programmatically added) appearing in front and in another ?

I want people who have the highest Y value in front of people with low Y values.

Thank you!

  Private function buyWorker (event: MouseEvent): Zero {// random Y Pod random YPos = Math.floor (Math.random () * 400); // Check whether the player can afford it if (money amount> gt; = workerCost) {clicksound.play (); // Reduce the money of the cost- zodiac = employee; Var worker: worker = new employee (); // worker addBild (worker); (Var i: int = 0; i & lt; (Root as Movie Clip) .Num Children; i ++) {var Children: DisplayObject = (Original as Movie Clip) .getChildAt (i); If (child is a worker) {workerArray.push (child); }} // Define the workers status worker. X = -50; Worker.y = yRange (140, 520); } And {erroround.play ();}}  

y property (You can also add sorting by x , just modify ["y"] -> [" Y "," x "] , therefore it would be suitable for iMetric objects, for example, they are all of the same size, for example 1x1 tile):

  Public static function sort child (container: display object container): zero {console B (I = l; len; i ++) for array i Kids. Push (Container .getChildAt (i)); Children.sortOn (SortOnopops, Array.NLERC); Var Children: DisplayObject; I = 0; While (i & lt; len) {child = DisplayObject (children [i]) ; Container .setChildIndex (child, i); i ++;}}  

Test:

  var Container: Sprite = Adbold (as Sprite New Sprite (); Var sh: Shape; For (var i: int = 0; i <100; i ++) {sh = new size (); Sh.graphics.beginFill (0xFFFFFF * Math.random (), 1); Sh.graphics.drawCircle (50, 50, 50); Sh.graphics.endFill (); Container.addChild (sh); Sh.x = int (Math.random () * 500); Sh.y = int (Math.random () * 500); } Sort child (container);  

Result:

Enter image details here


Comments