首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Flash >

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

2012-11-23 
flash as3.0粒子效果实例教程1、新建Flash文档,设置:宽、高为 400 × 400 ,保存。2、用椭圆工具在舞台上画一个

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

1、新建Flash文档,设置:宽、高为 400 × 400 ,保存。
2、用椭圆工具在舞台上画一个 20 × 20 大小的圆。 (你能选择任意的颜色)
3、右键单击圆形,把它转换成影片剪辑,注册点居中。
4、在ActionScript导出的复选框中打勾 ,做类链接,类名为" Particle " 。图1:

5、把圆形从舞台删除,新建ActionScript 3.0文件。图2:

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:

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:

?
?
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?
附件下载:

Particle.rar
??????

粒子.rar
本文转自:http://www.5uflash.com/flashjiaocheng/Flash-as3-jiaocheng/5300.html

热点排行