Random Pixels Blink White for 1/60th of a Second

2019-07-25 12:33发布

问题:

I am trying to make single pixels flash for 1/60th of a second and then go away over a 2 second period of time until every single pixel on a 1280x720 screen has flashed white. After 2 seconds have elapsed the screen is all black again for 3 or so seconds before it loops and does it again.

The way I solved it was using this fla another stackoverflow user came up with and I modified that uses movie clips. The problem is it doesn't work to get 921600 movie clips to start randomly. It gets really heavy and slow. See attached file that works with

Anyway! I'm sure there is a super smart way of doing this. I'm a novice. Thanks for any help or suggestions.

fla (cs5) https://mega.co.nz/#!ERRFiJBJ!VYSaH164BcjD9QIiSdpk8WxFp68dYDC0vWzKySC8rg0

swf https://mega.co.nz/#!kBoxmJCR!Mx7sHX94-9ch15dKdT8knHRRKRljytZXdOBK-2P-TLQ

best, Rollin

For the original design of the fla I'm linking to above see the solution by Mahmoud Abd El-Fattah on this link. Random Start Times for Move Clips

回答1:

Okay, the simplest method will be something like this:

static const WIDTH:int=1280;
static const HEIGHT:int=720;
static const WH:int=WIDTH*HEIGHT;
static const FRAMES:int=120; // 2 seconds * 60 frames. Adjust as needed
static var VF:Vector.<int>; // primary randomizer
static var BD:BitmapData; // displayed object
static var curFrame:int; // current frame
static var BDRect:Rectangle;
function init():void {
    // does various inits
    if (!VF) VF=new Vector.<int>(WH,true); // fixed length to optimize memory usage and performance
    if (!BD) BD=new BitmapData(WIDTH,HEIGHT,false,0); // non-transparent bitmap
    BDRect=BD.rect;
    BD.fillRect(BDRect,0); // for transparent BD, fill with 0xff000000
    curFrame=-1;
    for (var i:int=0;i<WH;i++) VF[i]=Math.floor(Math.random()*FRAMES); // which frame will have the corresponding pixel lit white
}
function onEnterFrame(e:Event):void {
    curFrame++;
    BD.lock();
    BD.fillRect(BDRect,0);
    if ((curFrame>=0)&&(curFrame<FRAMES)) {
        // we have a blinking frame
        var cw:int=0;
        var ch:int=0;
        for (var i:int=0;i<WH;i++) {
            if (VF[i]==curFrame) BD.setPixel(cw,ch,0xffffff);
            cw++; // next column. These are to cache, not calculate
            if (cw==WIDTH) { cw=0; ch++; } // next row
        }
    } else if (curFrame>FRAMES+20) {
        // allow the SWF a brief black period. If not needed, check for >=FRAMES
        init(); 
    }
    BD.unlock();
}
function Main() {
    init();
    addChild(new Bitmap(BD));
    addEventListener(Event.ENTER_FRAME,onEnterFrame);
}