A few days ago an article, with Flash easy to make electronic divergence visual effects of animation Many people ask what is the use? Let's continue today! Take a look at the infrared guided missile blast effect.
Explosion code We also use the code used in the previous tutorial, in this effect, we first change the shape of the cursor, and then click the mouse to have a missile appeared, and then exploded.
First look at the effect: (click the mouse to see the effect)
Create a new file, press Ctrl+j to resize and background color (#999999) and frame frequency.
Then make a small red circle film clip as the mouse shape, make a colored line film clip as a missile, and then make a small white circle as an exploding movie clip.
Then add the following code to the first frame.
/* GLOBAL VARIABLES * *
_global.gleft = 20;
_global.gtop = 20;
_global.gright = stage.width-20;
_global.gbottom = stage.height-20;
/* Function:converts radians to degrees * *
function Rad2deg (radians:number): number {
return radians * 180/MATH.PI;
}
/* Function:converts degrees to radians * *
function Deg2rad (degrees:number): number {
return degrees * MATH.PI/180;
}
/* Function:returns a random number between Min and MAX (inclusive) * *
function Randrange (Min:number, Max:number): number {
var randomnum:number = Math.floor (Math.random () * (Max-min + 1)) + min;
return randomnum;
}
/* Function:creates an explosion * *
Explosion.maxspeed = 25;
Explosion.minsize = 4;
Explosion.maxsize = 10;
Explosion.minfragments = 10;
Explosion.maxfragments = 50;
function explosion (Originx:number, Originy:number): Void {
var totalfragments:number = Randrange (explosion.minfragments, explosion.maxfragments);
var Fragment_mc:movieclip;
var depth:number = this.getnexthighestdepth ();
for (var i:number = 0; i < totalfragments; i++, depth++) {
FRAGMENT_MC = Attachmovie ("Fragment", "fragment" + depth, depth);
fragment_mc._x = Originx;
fragment_mc._y = Originy;
Fragment_mc._width = Fragment_mc._height = Randrange (explosion.minsize, explosion.maxsize);
while (!fragment_mc.speedx) {
Fragment_mc.speedx = Randrange (-explosion.maxspeed, explosion.maxspeed);
}
while (!fragment_mc.speedy) {
Fragment_mc.speedy = Randrange (-explosion.maxspeed, explosion.maxspeed);
}
Fragment_mc._alpha = Randrange (10, 100);
Fragment_mc.cacheasbitmap = true;
Fragment_mc.onenterframe = function (): Void {
This._x + = This.speedx;
This._y + = This.speedy;
if (this._x < Gleft | | this._x > Gright | | this._y < gtop | | this._y > Gbottom) {
This.removemovieclip ();
}
};
}
}
/* Function:creates and initializes heat-seeking missiles * *
Createmissile.maxspeed = 30;
Createmissile.accel = 2;
function Createmissile (Target_mc:movieclip, Xpos:number, Ypos:number): Void {
var depth:number = _root.getnexthighestdepth ();
var missile_mc:movieclip = Attachmovie ("Missile", "missile" + depth, depth);
missile_mc._x = xpos;
missile_mc._y = YPos;
missile_mc.speed = 0;
Missile_mc.setrotation = function (): Void {
var dy:number = target_mc._y-this._y;
var dx:number = target_mc._x-this._x;
This._rotation = rad2deg (math.atan2 (dy, dx));
};
Missile_mc.onenterframe = function (): Void {
This.setrotation ();
if (This.speed < createmissile.maxspeed) {
This.speed + = Createmissile.accel;
}
This._x + + math.cos (Deg2rad (this._rotation)) * this.speed;
This._y + + Math.sin (Deg2rad (this._rotation)) * this.speed;
if (This.hittest (TARGET_MC)) {
Explosion (target_mc._x, target_mc._y);
This.removemovieclip ();
}
};
}
/* MAIN Program LOGIC * *
var gamebg_mc:movieclip = _root.createemptymovieclip ("GAMEBG", 0);
Gamebg_mc.beginfill (0x222222);
Gamebg_mc.moveto (Gleft, gtop);
Gamebg_mc.lineto (Gright, gtop);
Gamebg_mc.lineto (Gright, Gbottom);
Gamebg_mc.lineto (Gleft, Gbottom);
Gamebg_mc.endfill (Gleft, gtop);
var target_mc:movieclip = Attachmovie ("target", "Target", _root.getnexthighestdepth ());
var startx:number = (gright-gleft)/2 + gleft;
var starty:number = (gbottom-gtop)/2 + gtop;
StartDrag (TARGET_MC, true);
Mouse.hide ();
OnMouseDown = function (): Void {
Createmissile (TARGET_MC, StartX, starty);
};