I have some problems with my time based animation. I was trying to make animation with bouncing ball which is devided into two smaller balls everytime it touches the edge of the board. However only first ball is animated, the rest is not moving at all. Here is my code
public function createBall( rad:int = 20, posX:int = 50, posY:int = 50 ):void
{
ball = new Ball( rad );
ballsArray.push( ball );
ball.x = posX;
ball.y = posY;
ball.speedX = - ball.range( 200, 50 );
ball.speedY = - ball.range( 200, 50 );
board.addChild( ball );
}
public function onEnterFrame( event:Event ):void
{
for ( var i:int = 0; i < ballsArray.length; ++i )
{
var ball:Ball = ballsArray[ i ];
moveBalls( ball );
}
}
public function moveBalls( ball:Ball ):void
{
ball.radius = ball.width / 2;
var elapsed:int = getTimer() - time;
time = getTimer();
offset = elapsed / 1000;
trace( offset );
ball.newPosX = ball.x + ball.speedX * offset;
ball.newPosY = ball.y + ball.speedY * offset;
if ( ball.newPosX + ball.radius > board.width )
{
//changeX *= -1;
ball.speedX *= - 1;
ball.newPosX = board.width - ball.radius;
if ( ( ballsArray.length < ball.BALLS_LIMIT ) && ( Math.random() < ball.PROBABILITY ) )
{
ball.width /= 2;
ball.height /= 2;
createBall( ball.radius / 2, ball.newPosX, ball.y );
}
}
else if ( ball.newPosX - ball.radius < 0 )
{
//changeX *= -1;
ball.speedX *= - 1;
ball.newPosX = ball.radius;
}
if ( ball.newPosY + ball.radius > board.height )
{
ball.speedY *= - 1;
//changeY *= -1;
ball.newPosY = board.height - ball.radius;
}
else if ( ball.newPosY - ball.radius < 0 )
{
ball.speedY *= - 1;
//changeY *= -1;
ball.newPosY = ball.radius;
}
ball.x = ball.newPosX;
ball.y = ball.newPosY;
}