开发者

How to re-position the children of a MovieClip?

I am having a container mc with 5 children mcs.

children names mc0,mc1....mc4.

  开发者_如何学JAVA  cont.getChildByName("mc"+Number(cont.numChildren-1)).x = 
            cont.getChildByName("mc0").x - 20 *1.2;
 

after this re-position process.. I want to set the last item position as 0 and so on. How can I do this?

My target is to attain a circular movement.

like

      [mc0][mc1][mc2]
      [mc2][mc0][mc1]
      [mc1][mc2][mc0]
      [mc0][mc1][mc2]


//Of course, you don't necessarily have to create absolute positions,
//this is a simple example...
var positions:Array = [{x:0,y:0} , {x:20, y:20} etc....];
var children:Array = [mc0 , mc1 ... mcN];

//Provided that positions & children have the same length
private function rotate():void
{
    //remove the last element of the Array
    var lastChild:MovieClip = children.pop();

    //Add it to the beginning of the Array
    children.unshift(lastChild );

     //Assign new positions
    //Here you could tween for smoother effect
    for( var i:int ; i < positions.length ; ++i )
    {
      children[i].x = positions[i].x;
      children[i].y = positions[i].y;
    }
}


Let's introduce an offset variable simulating the rotation's progression:

var offset:uint = 0;

Now we must define each clip's position depending on this variable. I will introduce a gap constant for the distance between two items.

const GAP:uint = 20;
for (var iMc:int=0; iMc < cont.numChildren; iMc++)
{
    mc = cont.getChildByName("mc" + iMc.toString()) as Sprite;
    mc.x = GAP * ((iMc + offset) % cont.numChildren);
}

The % operator (modulo) allows you to get a number between 0 and the second operand-1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜