AS3虚拟粒子阵列

发布于 2024-10-26 14:31:57 字数 2282 浏览 5 评论 0原文

我不久前按照教程制作了一个粒子类。基本上它使用某种虚拟列表来替换数组方法,这使得它成为真正快速的解决方案。所以一切都工作得很好,除了我真的不明白到底发生了什么。这非常令人困惑,所以我试图找到这些东西的逻辑,不幸的是没有成功。如果有人能真正解释这一点,我会很高兴,这样我就可以把它变成有用的东西。

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();
        }
    }

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();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2024-11-02 14:31:57

这称为链表。请参阅此处

对于此应用程序来说,更好的结构可能是向量。这是 AS3 中的一种数组数据类型,针对像您所做的那样的顺序访问进行了优化。由于粒子数量是固定的,因此不必担心调整 Vector 大小的成本。当您需要添加或删除不在列表末尾的元素,或者当您需要大量可变的元素时,链接列表非常有用。

使用这样的向量:

// 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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文