flash as3.0粒子效果实例教程

1、新建Flash文档,设置:宽、高为 400 × 400 ,保存。
2、用椭圆工具在舞台上画一个 20 × 20 大小的圆。 (你能选择任意的颜色)
3、右键单击圆形,把它转换成影片剪辑,注册点居中。
4、在ActionScript导出的复选框中打勾 ,做类链接,类名为" Particle " 。图1:
[img]/uploads/allimg/100129/2223470.png[/img]
5、把圆形从舞台删除,新建ActionScript 3.0文件。图2:
[img]/uploads/allimg/100129/2223471.png[/img]
6、我们编写一个外部的Particle类。在编译器中输入代码:
package {
        import flash.display.MovieClip;
        public class Particle extends MovieClip {
                //We need different speeds for different particles.
                //These variables can be accessed from the main movie, because they are public.
                public var speedX:Number;
                public var speedY:Number;
                public var partOfExplosion:Boolean = false;
                function Particle ():void {
                }
        }
}7、保存在fla文件的同一目录下,名为 " Particle " 。图3:
[img]/uploads/allimg/100129/2223472.png[/img]
8、切换到我们的fla主文档。首先我们在舞台上生成粒子实例。在第一帧输入代码:
//We need few imports for the color
import fl.motion.Color;
import flash.geom.ColorTransform;
/*We want 20 particles at the start
particlesArray is used when we animate each particle */
var numberOfParticles:Number = 20;
var particlesArray:Array = new Array();
//Each time a hit occurs, we want to create 10 new particles
var numberOfExplosionParticles:uint = 10;
//This loop creates the first particles and gives them speed and coordinates
for (var i=0; i 
9、测试你的影片,效果如图。图4:
[img]/uploads/allimg/100129/2223473.png[/img]
 
 
10、随机地选择一个粒子产生爆炸效果。爆炸后,生成新的粒子。最后,删除舞台上爆炸的粒子。把下列代码块加入到动作面板: //Call for the first explosion
startExplosions ();
/*This function makes a random particle to explode.
From here, the chain reaction begins.*/
function startExplosions ():void {
        //Select a random particle from an array
        var index = Math.round(Math.random() * (particlesArray.length-1));
        var firstParticle:Particle = particlesArray[index];
        //Set a random tint
        var ct:Color = new Color();
        ct.setTint (0xFFFFFF * Math.random(),1);
        //Create 10 new particles because of explosion
        for (var i=0; i 11、添加方法 enterFrameHandler,更新粒子坐标,使粒子动起来。输入下列代码:
//This function is responsible for the animation
function enterFrameHandler (e:Event):void {
        //Loop through every particle
        for (var i=0; i 12、方法 " checkForHit" 是最难的部份,碰撞检测。输入代码:
/*This function checks whether two particles have collided*/
function checkForHit (particleOne:Particle, particleTwo:Particle):void {
        /*Let’s make sure we only check those particles, where one is moving and the other
        is stationary. We don’t want two moving particles to explode. */
        if ((particleOne.partOfExplosion == false && particleTwo.partOfExplosion == true) ||
        particleOne.partOfExplosion == true && particleTwo.partOfExplosion == false ) {
                //Calculate the distance using Pythagorean theorem
                var distanceX:Number = particleOne.x - particleTwo.x;
                var distanceY:Number = particleOne.y - particleTwo.y;
                var distance:Number = Math.sqrt(distanceX*distanceX + distanceY*distanceY);
                /* If the distance is smaller than particle’s width, we have a hit. 
                Note: if the particles were of different size, the calculation would be:
                distance 13、代码全部完成,测试你的影片。也可以设置不同背景的舞台,画任意的图形。
最后完整的代码:
//We need few imports for the color
import fl.motion.Color;
import flash.geom.ColorTransform;
/*We want 20 particles at the start
particlesArray is used when we animate each particle */
var numberOfParticles:Number = 20;
var particlesArray:Array = new Array();
//Each time a hit occurs, we want to create 10 new particles
var numberOfExplosionParticles:uint = 10;
//This loop creates the first particles and gives them speed and coordinates
for (var i=0; i 
附件下载:
[img]/uploads/allimg/100129/2223474.gif[/img]
[url=http://www2.flash8.net/UploadTeach/201001/2010012918328666.rar]Particle.rar[/url]
      
[img]/uploads/allimg/100129/2223474.gif[/img]
[url=http://www2.flash8.net/UploadTeach/201001/2010012918343303.rar]粒子.rar[/url]
本文转自:http://www.5uflash.com/flashjiaocheng/Flash-as3-jiaocheng/5300.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值