| 爆炸的代码我们还使用前面那个教程中用到的代码,在这个效果里,我们首先把光标的形状变换一下,然后点鼠标就有一个导弹出现,然后发生爆炸。
新建立一个文件,按Ctrl+J调整大小和背景色(#999999)和帧频。

然后制作一个红色的小圆影片剪辑作为鼠标的形状,制作一条有色的线条影片剪辑作为导弹,然后再制作一个白色小圆作为爆炸的影片剪辑。
然后在第一帧加入如下代码。
/* 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); };
|