开发者

AS3 virtual array of particles

开发者 https://www.devze.com 2023-02-20 04:37 出处:网络
I made a particle class following a tutorial a while ago. Basically it uses some sort of a virtual list, replacing the array method, which makes it really fast solution. So everything is working perfe

I made a particle class following a tutorial a while ago. Basically it uses some sort of a virtual list, replacing the array method, which makes it really fast solution. So everything is working perfectly fine except the fact that i really can't understand what actually is going on. It is pretty confusing so im trying to find the logic in this stuff, unfortunately with no success. I will be glad if someone can really explain this , so i can get it to something usefull.

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import Part;
    import FPSCounter;

    public class Pixels extends Sprite
    {
        private var num:int = 500000;
        private var sw:Number = stage.stageWidth;
        private var sh:Number = stage.stageHeight;
        private var bdata:BitmapData=new BitmapData(sw,sh,false,0x111111);
        private var bmp:Bitmap = new Bitmap(bdata);
        private var firstParticle:Part;
        private var radius:Number;
        private var range:Number;
        private var color:uint = 0xffffff;

        public function Pixels()
        {
            addChild(bmp);
            addChild(new FPSCounter());
            createParticles();
            addEventListener(Event.ENTER_FRAME,anim);
        }

        private function createParticles():void
        {
            var lastParticle:Part;

            for (var i:int = 0; i < num; i++)
            {
                radius = Math.random() * (2 * Math.PI);
                range = Math.random() * (2 *开发者_高级运维 Math.PI);
                var thisP:Part = new Part;
                thisP.x = sw / 2;
                thisP.y = sh / 2;
                thisP.xvel=Math.sin(range) * radius;
                thisP.yvel = Math.cos(range) * radius;

                if (i == 0) 
                {
                    firstParticle = thisP;                      }

                else
                {
                    lastParticle.next = thisP; // ???? 
                }

                lastParticle = thisP; 
            }
        }

        private function anim(event:Event):void
        {
            var p:Part = firstParticle;
            bdata.lock();
            p = firstParticle;
            bdata.fillRect(bdata.rect, 0x111111);

            do 
            {
                p.y += p.xvel;
                p.x += p.yvel;

                bdata.setPixel(p.x, p.y, color);
                p = p.next;
            } 
            while (p != null)

            bdata.unlock();
        }
    }


This is called a linked list. See here.

A better structure for this application might be a Vector. This is an array data type in AS3 optimized for sequential access like you're doing. Since you have a fixed number of particles, you don't have to worry about the cost of resizing the Vector. Linked lists are useful in cases where you need to add or delete elements that are not at the end of the list, or when you need a highly variable number of elements.

Use a Vector like this:

// This is a member variable
private var particles : Vector.<Part> = new Vector.<Part>(num);

// This fills the particle list
for(var ii : int = 0; ii < num; ++ii) {
    var part : Part = new Part();
    // Initialize the particle here
    particles.push_back(part);
}

// This iterates through the particles
for each(var part : Part in particles) {
    // Draw part
}
0

精彩评论

暂无评论...
验证码 换一张
取 消