I got the AS code for a really cool particle fountain and modded it to use it for the animation of a popcorn machine. It turned out really great. The only thing I didn’t care for was that the particles were balls. It worked fine on a small graphic but now i want to make a larger scale version and wanted to make the sprites actual pieces of popcorn.
I’m still really new to AS3 and was wondering if anyone could point me in the right direction. : )
Here’s the code I’m using so far:
/*
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;
}
}