PDA

View Full Version : lightning efects



dbh_21
12-20-2003, 11:50 AM
how would i create lightning effects in flash

Sualdam
12-20-2003, 05:24 PM
I emphasise that this isn't my coding - it is something I picked up off the web, but I can't remember where.

The credit must go to whoever wrote it.

Paste the code into the first frame of an empty movie. Set the background colour to black (or dark). Then test it - click the mouse to trigger the effect.


function makeLightning () {

}
max_sub_branch = 4 ;
max_sub_angle = 3*Math.PI/5 ;
max_size = 6 ;
length = 10 ;

function makeBranch ( targ, start_x, start_y, angle, size ) {
if (size > 0 ) {
c++ ;
var cl = targ.createEmptyMovieClip ("light" + c, c) ;
cl.lineStyle ( size, 0xffffff, 100 ) ;
cl.moveTo ( start_x, start_y ) ;
while (start_y < 400) {
var end_x = start_x + length * Math.cos ( angle ) + random(10) - 5 ;
var end_y = start_y + length * Math.sin ( angle ) //+ random(10) - 5 ;
cl.lineTo ( end_x, end_y ) ;
start_x = end_x ;
start_y = end_y ;

if ( Math.random() > .9 && cl.sub < max_sub_branch ) {
cl.sub ++ ;
var newLength = length -1 ;
var newAngle = Math.PI/4 + Math.random() * Math.PI/2 ;
var newSize = size - 1 ;
makeBranch ( cl, end_x, end_y, newAngle, newSize ) ;
}
}
}
}

this.onMouseDown = function () {
c = 0 ;
this.light1.removeMovieClip () ;
makeBranch ( this, 250, 0, Math.PI/6 + Math.random() * 2/3*Math.PI, max_size ) ;
this.light1.onEnterFrame = function () {
if ((this._alpha -= 5) < 5) this.removeMovieClip () ;
}
}