Monday, November 18, 2013

Floating bubbles [Actionscript 3]

  • Open Flash new document.
  • Create a small ball shape movieclip in the library.
  • Give it a AS linkage name "Ball".

What we are going to do :
  • Creating the ball. [Random place, Random Size, Random Color ...].
  • Displaying the ball on stage.
  • Moving the ball. 
 <code>


//stage size = 926 x534 px
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.ColorTransform;

var Bx;
var By;
var Bd;
var Ba;
var red;
var green;
var blue;


/*

  Assigning variables
        ball x position = Bx
        ball y position = By
        ball diameter   = Bd
        ball opacity    = Ba
   
        red   = red color amount of the ball
        green = green color amount of the ball
        blue  = color amount of the ball

*/



function createBall():void{
    Bx = Math.floor(Math.random()*926)+1; 

//Max value = 926 Min value = 1

    By = -Math.floor(Math.random()*534)-54;

//according to the values, ball creates above the stage

    Bd = Math.floor(Math.random()*20)+10;
    Ba = Math.floor(Math.random()*99)+60;
    var ball:Ball = new Ball();
    ball.x = Bx;
    ball.y = By;
    ball.width = Bd;
    ball.height = Bd;
   
   
    red = Math.random();
    green = Math.random();
    blue = Math.random();
    var myColour:ColorTransform = new  
    ColorTransform(red+0.6,green,blue-0.2);
    ball.transform.colorTransform = myColour;
    ball.alpha = Ba/100;
    var xposInf = Math.floor(Math.random()*5)-2;
    var yposInf = Math.floor(Math.random()*5)-2;
    function moveBall(e:Event):void{
        ball.x+=xposInf;
        ball.y+=yposInf;
    }

// Adding the ball on stage   
    stage.addChild(ball);
//Moving the ball according to frame rate.   
    stage.addEventListener(Event.ENTER_FRAME,moveBall);
/*
To save the memory usage
after 60 seconds ball created will be removed
*/

    var timer:Timer = new Timer(60000,1);
    timer.addEventListener(TimerEvent.TIMER,removeBall);
    function removeBall(e:Event):void{
        stage.removeChild(ball);
    }
    timer.start();
}


/*
    A function to create a ball
        Ball x,y,size,opacity values generates randomly as well as color value.
        xposInf is a variable for horizontal(x) speed of the ball (random value)
        yposInf is a variable for vertical (y) speed of the ball (random value)
     
*/


// A function to create 2 balls at a time
function addBalls(e:Event):void{
    createBall();
    createBall();
}

// creating two balls on stage continuously
stage.addEventListener(Event.ENTER_FRAME,addBalls);
//adding a significant amount(100) balls at the start
for(var i=0;i<100;i++){
    createBall();
}






No comments:

Post a Comment