AS3上的粒子系统,使用flash,复制粒子元素的问题

时间:2011-01-11 05:43:27

标签: flash actionscript-3 particles

我在AS3中开发了一个非常简单的粒子系统,我有粒子(动画片段)和粒子行为,但是现在我需要一个很好的方法来复制它多次并改变决定系统行为的唯一值,宽度,从10到100像素。

这是代码:

//some declarations
var blur:BlurFilter = new BlurFilter();
var filterArray:Array = new Array(blur);
import fl.transitions.Tween;
import fl.transitions.easing.*;

//the only input value, from 10 to 100
par.width=100;
//the equations that define the behavior.
par.alpha=.0088*par.width+.98;
par.height=par.width;
blur.blurX = .75*par.width-.55;
blur.blurY = blur.blurX;
blur.quality = 1;
par.filters = filterArray;
//the movement of the particle
var myTween:Tween = new Tween(par, "y", Strong.easeOut, par.y, stage.stageHeight+2*par.height, -.2*par.width+22, true); 

所以,正如你所看到的, par 是粒子的实例名称,我需要复制它,改变.width值,最后也改变.x值。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

这就是OOP(面向对象编程)的全部内容,Flash就是一个很好的例子。

package  {

    import flash.filters.BlurFilter;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Particle extends MovieClip {

        public function Particle() {
            // constructor code
            //some declarations
            this.graphics.beginFill(0, 1);
            this.graphics.drawCircle(0, 0, 50);
            var blur:BlurFilter = new BlurFilter();
            var filterArray:Array = new Array(blur);
            //the only input value, from 10 to 100
            this.width = Math.round(Math.random() * 90) + 10;
            //the equations that define the behavior.
            this.alpha = .0088 * this.width + .98;
            this.height = this.width;
            blur.blurX = .75 * this.width - .55;
            blur.blurY = blur.blurX;
            blur.quality = 1;
            this.filters = filterArray;
            this.addEventListener(Event.ADDED_TO_STAGE, __tweenMe);
        } 


        private function __tweenMe($evt:Event):void {
            //the movement of the particle
            var myTween:Tween = new Tween(this, "y", Strong.easeOut, this.y, stage.stageHeight+2*this.height, -.2*this.width+22, true); 
        }

    }

}

然后在你的DocumentClass中你可以这样做:

package  {

    import flash.display.MovieClip;

    public class BaseClass extends MovieClip {

        public function BaseClass() {
        var par:Particle;
            for ( var i:int = 0; i < 100; i++) {
                par = new Particle();
                addChild(par);
            }       
        }   
    }    
}

编辑

在这里你http://d.pr/ycUh。如果您对正在发生的事情有疑问,请与我们联系。我为粒子的起始位置添加了一些随机的x和y值。