Heres another neat effect that you can use in your next flash game or movie:
ELECTRICITY
Here's an example of what this does. Click to toggle between sparks and electricity. (FPS: 30, BG: Black, QUALITY: LOW)
This goes in an .as file named Static.as :
Note: This will only work in ActionScript 2.0!!!
import flash.geom.Point;
class Static extends MovieClip {
private var elec:MovieClip;
private var rand:Number;
private var i:Number;
function Static() {
elec = this.createEmptyMovieClip("elec", 1);
}
public function init(x:Number, y:Number):Void {
MovieClip.prototype.drawPath = function() {
this.createEmptyMovieClip("path",2);
this.path.lineStyle(1,0xFFFFFF);
this.path.moveTo(this.x,this.y);
for (i=0; i<this.paths.length; ++i) {
this.path.lineTo(this.paths[i].x-this._x,this.paths[i].y-this._y);
}
};
MovieClip.prototype.addPath = function(x:Number, y:Number) {
if (this.paths.length<15) {
this.paths.unshift(new Point(x, y));
} else {
this.paths.pop();
this.paths.unshift(new Point(x, y));
}
};
MovieClip.prototype.move = function() {
this.addPath(this.x-this._x,this.y-this._y);
this.drawPath();
this.x += Math.cos(this.r*Math.PI/180)*this.spd;
this.y += Math.sin(this.r*Math.PI/180)*this.spd;
this.spd *= .5;
if (this.x>275+Stage.width/2 || this.x<275-Stage.width/2) {
this.r *= -1;
this.r += 180;
this.x = 275+Stage.width/2*(this.x>275+Stage.width/2)-Stage.width/2*(this.x<275-Stage.width/2);
}
if (this.y>200+Stage.height/2 || this.y<200-Stage.height/2) {
this.r *= -1;
this.y = 200+Stage.height/2*(this.y>200+Stage.height/2)-Stage.height/2*(this.y<200-Stage.height/2);
}
this.r += random(90)-random(90);
if (this.spd<.01) {
this.removeMovieClip();
}
if (this.spd<.5) {
if (random(2) == 0) {
this._visible = !this._visible;
}
}
};
elec.x = x;
elec.y = y;
elec.paths = new Array();
elec._rotation = random(360);
rand = random(10)+2;
elec.r = elec._rotation;
elec.spd = rand*7;
elec._rotation = 0;
elec.onEnterFrame = function() {
this.move();
};
elec.onUnload = function() {
this._parent.removeMovieClip();
};
}
}
For the .fla file :
1. Save your .fla in the same directory as the Static.as file
2. Go to Insert>>New Symbol...
3. Insert static for the name. Make sure the type is set to Movie Clip
4. Click Advanced if the advanced menu is not already being displayed. Under 'Linkage:' check 'Export for ActionScript'
5. The Identifier should read static. Enter Static into the AS 2.0 class area.
6. To add a bolt of static electricity at the mouse's position, use this code:
s = attachMovie("static", "static"+d, d);
s.init(_xmouse, _ymouse);
Here's an example of the static particle in use:
import flash.filters.GlowFilter;
_quality = "LOW";
var d:Number = 0;
var glow:GlowFilter = new GlowFilter(0x9900CC, 1, 16, 16, 6);
var hold:MovieClip = createEmptyMovieClip("hold", 1);
hold.filters = [glow];
onEnterFrame = function () {
for (i=0; i<3; ++i) {
s = hold.attachMovie("static", "static"+d, d);
s.init(_xmouse, _ymouse);
d++;
}
};
Electrifying!
If anybody is interested in this code snippet, why not develop your own particle effect and share it with the world? Perhaps we can get a collection going!
AS2 Flash Effect: Sparks