AS3虚拟粒子阵列
我不久前按照教程制作了一个粒子类。基本上它使用某种虚拟列表来替换数组方法,这使得它成为真正快速的解决方案。所以一切都工作得很好,除了我真的不明白到底发生了什么。这非常令人困惑,所以我试图找到这些东西的逻辑,不幸的是没有成功。如果有人能真正解释这一点,我会很高兴,这样我就可以把它变成有用的东西。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这称为链表。请参阅此处。
对于此应用程序来说,更好的结构可能是向量。这是 AS3 中的一种数组数据类型,针对像您所做的那样的顺序访问进行了优化。由于粒子数量是固定的,因此不必担心调整 Vector 大小的成本。当您需要添加或删除不在列表末尾的元素,或者当您需要大量可变的元素时,链接列表非常有用。
使用这样的向量:
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: