- 我正在通过AS3制作模拟器,我差点完成它,但是一些有效的问题困扰着我。
- 使用setPixel方法,重绘看起来很慢,我知道这不是AS3实现它的最好方法。
- 我只是想知道是否有更好的方法来加快它的速度?
for (var a:int = 0; a<8; a++)
{
color = 0;
if ( vram&1 ) color = 0x0000FF00;
setPixel(k, j, color);
k++;
vram = vram >> 1;
}
答案 0 :(得分:2)
我不确定这会加快多少速度,但将lock()和unlock()与copyPixels()结合使用而不是setPixel()应该更快。
首先创建一个1x2 BitmapData实例,您可以将其用作缓冲区。将最左边的像素设置为0x0,将最右边的像素设置为0x0000FF00。这样你就可以简单地从缓冲区复制适当的像素。
通过这种方式,您还可以消除为每次调用它所驻留的函数设置颜色变量8次的开销。
以下是我的意思的一般概念,如果您打算复制它,请不要忘记为alpha填写适当的值
// Instantiate these once only(!)
// Preferably in the constructor or an init functin
var buffer:BitmapData = new BitmapData(2,1);
var rect:Rectangle = new Rectangle(0,0,1,1);
var p:Point = new Point();
这是代码
lock();
for(var a:uint = 0; a < 8; a++)
{
// Select what color to copy, i.e. it's position in the buffer
rect.x = (vram&1) ? 1 : 0;
p.x = k; p.y = j;
copyPixels( buffer, rect, p );
k++;
vram = vram >> 1;
}
unlock();