PDA

View Full Version : keeping newest sprite on top



bobbywilson
July 19th, 2007, 10:28 PM
I am struggling at keeping the newest sprite that is created on an event at the top of the page. I want the previous sprites to move down incrementally as each new sprite is instantiated. I am missing something basic here, thanks for the help.

efnx
July 19th, 2007, 10:37 PM
That should happen automatically if you are using addChild(sprite), addChild() puts the child at the top of the display list, above all the other previous children.

CarlLooper
July 19th, 2007, 10:41 PM
Work "backwards".

From foreground sprites to background sprites.

Carl

I do it like this - advantage being that list is not displayed until last node is attached to to stage




// TIMELINE CODE - place in frame 1
//
// composing from leaf nodes (foreground)
// to root node (background)
//
// the following display list diagram is 'upside down' to emphasise this
//
//
// s3 s4 s5 s6 <--- foreground
// \ / \ /
// \/ \/
// s1 s2 <--- parents of foreground
// \ /
// \ /
// \/
// s0 <--- grandparent of foreground
// |
// root <--- background (main timeline)
// |
// stage
//
//


// construct an array of sprites
var s:Array = new Array();

for(var i:uint=0; i<7; i++)
{
s[i] = new Sprite();

// make each sprite identifyable by colour range
// where sprite 1 is near black
// and sprite 6 is blue

s[i].graphics.lineStyle(1,0xFFFF0000,1.0);
s[i].graphics.beginFill(0xFF000000 + Math.floor(i*255/7),1.0 );
s[i].graphics.drawRect(-50, -50, 100, 100);
s[i].graphics.endFill();

// set vertical position of each sprite relative to it's parent
// because, from our plan, we see that this is the
// same for every sprite

s[i].y = -55;

}

// for emphasis
// make sprite 0 bigger
// and change it's colour

s[0].graphics.lineStyle(1,0xFFFF0000,1.0);
s[0].graphics.beginFill(0xFF00bb44,1.0 ); // GREEN
s[0].graphics.drawRect(-140, -200, 280, 400);
s[0].graphics.endFill();

//--------------------------------------------------------------------
// start with leaf nodes adding them to what will then be their parent
//--------------------------------------------------------------------

//----------------
// leaf nodes
//----------------

s[3].x = -55;
s[4].x = 55;
s[1].addChild(s[3]);
s[1].addChild(s[4]);

s[5].x = -55;
s[6].x = 55;
s[2].addChild(s[5]);
s[2].addChild(s[6]);

//------------------------------------
// now the next level (closer to root)
//------------------------------------

s[1].x = -115;
s[2].x = 115;
s[0].addChild(s[1]);
s[0].addChild(s[2]);

//---------------------------------------------------
// and finally add the last node to the root
// at which point entire construction becomes visible
//---------------------------------------------------

s[0].x = 250;
s[0].y = 250;
addChild(s[0]);

bobbywilson
July 19th, 2007, 10:44 PM
Ah.. I'm sorry, I should have been more descriptive, by top, I meant the y-position. Not the display list.