actionscript 3 - Is there a way to keep an object always at the top of the display list? -


is there way make display object @ top of display list?

for example, know can set childindex, i.e:

setchildindex(mydisplayobject, numchildren-1);

but there way object has sensed else has been added display list , restack accordingly?

you can listen event.added event on container. event bubble up, you'll called whenever display object added container or of children.

here's example of how this. you'll see black box stays on top.

var container:displayobjectcontainer = this; // frame script change line necessary container.addeventlistener(event.added,handleadded,true);  function handleadded(e:event):void {     // if tries cover edge cases, unlikely happen in code, description     if(container == topelement.parent && container.numchildren > 0 && container.getchildindex(topelement) != container.numchildren - 1) {         container.setchildindex(topelement,numchildren - 1);     } }  function getsprite(c:uint):sprite {     var sp:sprite = new sprite();     sp.graphics.beginfill(c);     sp.graphics.drawrect(0,0,100,100);     sp.graphics.endfill();     return sp; }  var topelement:sprite = getsprite(0);  container.addchild(topelement); var sp:sprite = getsprite(0xff0000); container.addchild(sp); sp.addchild(getsprite(0xff00)); var sp2:sprite = getsprite(0xff); container.addchild(sp2); 

however, think it's simpler , cleaner have 2 containers, say, top , bottom, kind of layers. in top you'd add element must on top (or element don't need have container). in bottom you'd add , remove whatever want. can forget manually restacking stuff (at least keep top element atop).


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -