Home / Tutorials / ActionScript / Laser Show /

Flash Tutorials

Laser Show - Writing the code

Posted by : Techload on Jan 25, 2011

 

5.0/5

Hi there and welcome to another tutorial !

First open a New File ( CTRL +N ) in Flash and select the Flash File ( Actionscript 3.0 )

Now, in the first frame hit F9 key so you can bring up the ActionScript Editor.

Copy & paste the code bellow, then I'll explain what you've just done.

var _laser:Sprite = null;

function Tutorial() 
{
   _laser = new Sprite();
   _laser.filters = [new BlurFilter(2,2,1), new GlowFilter(0x4411ff, 1, 32, 32, 8, 3)];

   addChild(_laser);

   addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
}

function Update(e:Event):void
{
  var xx:Number = stage.stageWidth/2;
  var yy:Number = 0
  var th:Number = Random(2) +1
  _laser.graphics.clear();
  _laser.graphics.moveTo(xx, yy);

  while(yy < stage.stageHeight)
  {
     xx += Random(20) * (Random(20)*2 -1);
     yy += Random(30) + 4    ;
     _laser.graphics.lineStyle(th,0xffffff,1);
     _laser.graphics.lineTo(Random(stage.stageWidth),stage.stageHeight);
  }
}

function Random(number:int):int
{
    return Math.floor(Math.random()* number);
}

Tutorial();

Ok .. so what you've just done you ask ?

First we created a Sprite object on which we've applied 2 filters: glow and blur.

After that we've added the object on the scene.

The Update() function will update the scene every frame. Inside the Sprite object will be cleared and redrawn each frame taking random values ( only the position of the end line ).

This is pretty much it ! Run the code and see what is happening.


Thanks for checking out this tutorial and hope you found it useful an easy to follow. If you have questions or just want to let me know it worked for you, drop a comment below anytime.

Visit my portfolio to see what else I'm working on.

Techload

no comment

Add comment

Please login to post comments.