I got this AS3 code about a year ago off a website and was able to modify it enough to use it for a banner showing popping pocorn. Now I'm looking to change it so instead of balls I can use instances of a movie clip (a piece of popcorn). I've been trying to replace the drawn ball with a MC but have had zero luck so far. I'm still really new to AS3 so any help would be greatly appreciated. Thanks!
Actionscript Code:
/*
Define the gravity.
This determines how fast the balls are pulled down.
*/
var gravity:Number = 0.2;
//Create 128 balls in the for-loop (change this to whatever you want)
for (var i = 0; i < 44; i++) {
//Create a ball
var ball:MovieClip = new MovieClip();
//Call the function that draws a circle on the ball movie clip
drawGraphics(ball);
//Initial position of the ball
ball.x = 55;
ball.y = 50;
//Define a random x and y speed for the ball
ball.speedX = Math.random() * 2 - 1;
ball.speedY = Math.random() * -4 - 2;
//Add the ball to the stage
addChildAt(ball, 2)
//ENTER_FRAME function is responsible for animating the ball
ball.addEventListener(Event.ENTER_FRAME,moveBall);
}
/*
This function is responsible for drawing a circle
on a movie clip given as parameter
*/
function drawGraphics (mc:MovieClip):void {
//Give a random color for the circle
mc.graphics.beginFill (0xffff99);
//Draw the cirlce
mc.graphics.drawCircle (0, 0, 1.5);
//End the filling
mc.graphics.endFill ();
}
//This function is responsible for animation a ball
function moveBall (e:Event):void {
//Let's assign the ball to a local variable
var ball:MovieClip = (MovieClip)(e.target);
//Apply gravity tot the y speed
ball.speedY += gravity;
//Update the ball's postion
ball.x += ball.speedX;
ball.y += ball.speedY;
//Chech if the ball has gone under the stage
if (ball.y > 109) {
/*
Calculate the mouse height.
We use the mouse height to give the ball a new random y speed
*/
var mouseHeight:Number = stage.stageHeight - 200;
//Calculate the new y speed
var newSpeedY = Math.random() * (-75 * 0.05);
//Move the ball to the initial position
ball.x = 55 ;
ball.y = 50 ;
//Assign the new calculated random speeds for the ball
ball.speedX = Math.random() * 4 - 1;
ball.speedY = newSpeedY;
}
}