Problem with create new MovieClip and move them

Hi,

I created 6 new MovieClips on the stage. I want to move them. The codes are:

// Create 6 balls
for (var i=0; i<=5; i++) {
var ball:MovieClip = new TestClip();
ball.name = “ball”+i;
ball.x = Math.random()*450;
ball.y = Math.random()*400;
addChild(ball);
}

stage.addEventListener(Event.ENTER_FRAME, moveBall);

function moveBall(evt:Event):void{
// when use ball.x one of the ball will move
ball.x += 2;
// However ball0.x, ball1.x … ball5.x not working with errors
}

I tried to use the function moveBall to control their movement. However it seems that their instance names are not ball0, ball1, ball2…ball5. Could you please provide some guidlines for me?

Thanks and best regards

Alex

evt.target.x += 2;

You don’t use the instance name reference explicitly in an event listener.

Hmm, on second reading… you’ll need to do things quite differently.

ball.x will refer to the most recently created ball, as above you are assigning a reference to a MC to “ball”. You can either create six separate ball references, or use getChildByName to get a reference to each ball, but that can be quite slow.

ball1 through ball6 exist in the display list - this isn’t like AS2 where MCs and the like all existed together with variables.

Hey alex298

You could put all your “balls” (hehe) in an array.
This would make it a lot easier for you to change stuff like .x or .width or whatever.
If you need some more information check this tutorial:
http://www.flepstudio.org/english/tutorials/mix/the-importance-of-an-array-20071029163.html

hth Carlo

What it looks like you want to use is the getChildByName() function. It takes a string and returns the child with the name parameter that matches.

Hi all,

Thanks for your help. I use getChildByName and it works :slight_smile:

Best regards

Alex