Salve a tutti, non sono un esperto di actionscript, ma ho trovato questo script fantastico che fa muovere nello stage dei MovieClip e scontrarli tra di loro variandone la posizione e velocità.
Vorrei però che invece di crearli in automatico, potesse prendere i MovieClip da un array, in modo che ciascuno di essi sia differente e non un clone del medisimo. Chi mi sa aiutare?
codice:
var left:Number = 0;
var right:Number = Stage.width;
var top:Number = 0;
var bottom:Number = Stage.height;

var numBalls:Number = 2;
for(var i:Number = 0;i<numBalls;i++)
{
	var ball:MovieClip = attachMovie("ball", "ball" + i, i);
	ball._x = Math.random() * Stage.width;
	ball._y = Math.random() * Stage.height;
	ball.vx = Math.random() * 10 - 5;
	ball.vy = Math.random() * 10 - 5;
	ball.mass = Math.random() * 150 + 20;
}

function onEnterFrame():Void
{
	for(var i=0;i<numBalls;i++)
	{
		var ball:MovieClip = this["ball" + i];
		ball._x += ball.vx;
		ball._y += ball.vy;
		checkWalls(ball);
	}
	for(var i=0;i<numBalls-1;i++)
	{
		var ballA:MovieClip = this["ball" + i];
		for(var j:Number = i+1;j<numBalls;j++)
		{
			var ballB:MovieClip = this["ball" + j];
			checkCollision(ballA, ballB);
		}
	}
}

function checkWalls(ball:MovieClip):Void
{
	if(ball._x < left + ball._width / 2)
	{
		ball._x = left + ball._width / 2;
		ball.vx *= -1;
	}
	else if(ball._x > right - ball._width / 2)
	{
		ball._x = right - ball._width / 2;
		ball.vx *= -1;
	}
	if(ball._y < top + ball._height / 2)
	{
		ball._y = top + ball._height / 2;
		ball.vy *= -1;
	}
	else if(ball._y > bottom - ball._height / 2)
	{
		ball._y = bottom - ball._height / 2;
		ball.vy *= -1;
	}
}

function checkCollision(ball0:MovieClip, ball1:MovieClip):Void
{
	var dx:Number = ball1._x - ball0._x;
	var dy:Number = ball1._y - ball0._y;
	var dist:Number = Math.sqrt(dx*dx + dy*dy);
	if(dist < ball0._width / 2 + ball1._width / 2)
	{
		// calculate angle, sine and cosine
		var angle:Number = Math.atan2(dy, dx);
		var sine:Number = Math.sin(angle);
		var cosine:Number = Math.cos(angle);
		
		// rotate ball0's position
		var pos0:Object = {x:0, y:0};
		
		// rotate ball1's position
		var pos1:Object = rotate(dx, dy, sine, cosine, true);

		// rotate ball0's velocity
		var vel0:Object = rotate(ball0.vx, ball0.vy, sine, cosine, true);
		
		// rotate ball1's velocity
		var vel1:Object = rotate(ball1.vx, ball1.vy, sine, cosine, true);

		// collision reaction
		var vxTotal:Number = vel0.x - vel1.x;
		vel0.x = ((ball0.mass - ball1.mass) * vel0.x + 2 * ball1.mass * vel1.x) / (ball0.mass + ball1.mass);
		vel1.x = vxTotal + vel0.x;
		
		// update position
		var absV:Number = Math.abs(vel0.x) + Math.abs(vel1.x);
		var overlap:Number = (ball0._width / 2 + ball1._width / 2) - Math.abs(pos0.x - pos1.x);
		pos0.x += vel0.x / absV * overlap;
		pos1.x += vel1.x / absV * overlap;
		
		// rotate positions back
		var pos0F:Object = rotate(pos0.x, pos0.y, sine, cosine, false);
		
		var pos1F:Object = rotate(pos1.x, pos1.y, sine, cosine, false);
		
		// adjust positions to actual screen positions
		ball1._x = ball0._x + pos1F.x;
		ball1._y = ball0._y + pos1F.y;
		ball0._x = ball0._x + pos0F.x;
		ball0._y = ball0._y + pos0F.y;
		
		// rotate velocities back
		var vel0F:Object = rotate(vel0.x, vel0.y, sine, cosine, false);
		var vel1F:Object = rotate(vel1.x, vel1.y, sine, cosine, false);
		ball0.vx = vel0F.x;
		ball0.vy = vel0F.y;
		ball1.vx = vel1F.x;
		ball1.vy = vel1F.y;
	}
}

function rotate(x:Number, y:Number, sine:Number, cosine:Number, reverse:Boolean):Object
{
	var result:Object = new Object();
	if(reverse)
	{
		result.x = x * cosine + y * sine;
		result.y = y * cosine - x * sine;
	}
	else
	{
		result.x = x * cosine - y * sine;
		result.y = y * cosine + x * sine;
	}
	return result;
}